0

Hi guys I have problem in Java. The problem is around parsing JSON with Jackson as I was instructed. My JSON is parsed well, that's not the problem. The problem lies in that I have multiple JSON items in one JSON. I've parsed it like this:

ObjectMapper objectMapper = new ObjectMapper();

try {
    List<Unit> unitList = objectMapper.readValue(json,List.class);

    System.out.println("UnitSize " + String.valueOf(unitList.size()));
    System.out.println(unitList.get(0).getUnitEmail());
} catch (IOException e) {
    e.printStackTrace();
}

and at UnitSize it'll tell me that I have precisely 5 objects of Unit type, which is okay, but when I want to get something out of the List it says me this:

Exception in thread "main" java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.reddatura.API.HTTPRequest$Unit

I've googled it, but nothing relevant. What should be the problem

EDIT:

here is my class snippet:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Unit
{

    @JsonProperty("unitid")
    int unitId;

    @JsonProperty("unitname")
    String unitName;

    @JsonProperty("unitlogo")
    String unitLogo;

    @JsonProperty("unitaddress")
    String unitAddr;
    //other fields, getters setters
    @JsonCreator
    public Unit()
    {

    }
}

I want to parse into this model

5
  • You might need to provide some more code. Where exactly are you casting ? Commented Oct 22, 2014 at 8:12
  • You can't cast a Unit to a list. You will have to iterate the list and get the objects one by one Commented Oct 22, 2014 at 8:13
  • 2
    Your List contains LinkedHashMap and not Unit Commented Oct 22, 2014 at 8:15
  • Try to see what contain unitList.get(0) in the Debug environment. See if it contains Unit or LinkedHashMap Commented Oct 22, 2014 at 8:18
  • Pedro, how can I achieve it with Jackson? Commented Oct 22, 2014 at 8:23

3 Answers 3

1

I think you are not casting correctly your json value:

Using jackson you could do the following:

List<Unit> myUnits = objectMapper.readValue(json, objectMapper.getTypeFactory().
            constructCollectionType(List.class, Unit.class));

Hope it helps :)

Sign up to request clarification or add additional context in comments.

8 Comments

I don't want to sound rude, but it does not work like this. Gives me error: Unrecognized field "unitid" (Class com.reddatura.API.HTTPRequest), not marked as ignorable when I want to parse it like this :/
print your json (or at least part of it) and see if the json is arriving correctly
I'm already printing my JSON and it is valid (ran thru a validator also)
and the fied unitid is getting value in every jsonobject?
yep, it is still there, hence it is a must field
|
1

try this

 ObjectMapper objectMapper = new ObjectMapper();

        try {
            Unit[] unit = objectMapper.readValue(jsonString, Unit[].class);
            Log.i("TEST", String.valueOf(unit.length)+" ******* "+unit[1].getUnitEmail()+" ******* "+unit[1].getUnitName());

            // or 
            List<Unit> unit1 = objectMapper.readValue(jsonString, new TypeReference<List<Unit>>() { });
            Log.i("TEST_1", String.valueOf(unit1.size())+" ****11111*** "+unit1.get(1).getUnitEmail()+" **1111111**** "+unit1.get(1).getUnitName());

        } catch (IOException e) {
            e.printStackTrace();
    }

Comments

0

do something like this :

JSONObject jObject = new JSONObject(jsonString);
            JSONArray jsonArray = jObject.getJSONArray("posts");
            for (int i = 0; i < jsonArray.length(); i++) {
                    int a = jsonArray.getJSONObject(i).getInt("a"),
                    String s =  jsonArray.getJSONObject(i).getString("b"));
            }

jsonString is String variable and must be Json syntax

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.