Liver
Liver

Reputation: 91

Parsing JSON Array

I'm trying to print company list.

My JSON object:

{
    "Name": "crunchify.com",
    "Author": "App Shah",
    "Company List": [
        "Compnay: eBay",
        "Compnay: Paypal",
        "Compnay: Google"
    ]
}

code:

public class ProductTypeParser {

    public  void parseJson(JSONObject jsonObject) throws ParseException {



        JSONObject object = (JSONObject) jsonObject;
        String name = (String) object.get("Name");
        System.out.println(name);

        String age = (String) object.get("Author");
        System.out.println(age);

        //loop array        
        JSONArray msg = (JSONArray) object.get("Company List"); 
        Iterator<String> iterator = msg.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }       
    }
}

Error:

SEVERE: Servlet.service() for servlet [contextServlet] in context with path [/SpringSafeHouseService2.0] 
  threw exception [Request processing failed; nested exception is 
    java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.json.simple.JSONArray] 
  with root cause java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.json.simple.JSONArray

Upvotes: 1

Views: 1850

Answers (4)

Silvinus
Silvinus

Reputation: 1445

Cast into ArrayList instead of JSONArray (because can be a JSONArray or an ArrayList or an implementation of List it's depends how you deserialize your string so use an ArrayList or the interface List ensure you can't have a Class cast exception):

JSONObject parser = new JSONParser();
    JSONObject object = (JSONObject) parser.parse("{\n" +
            "    \"Name\": \"crunchify.com\",\n" +
            "    \"Author\": \"App Shah\",\n" +
            "    \"Company List\": [\n" +
            "        \"Compnay: eBay\",\n" +
            "        \"Compnay: Paypal\",\n" +
            "        \"Compnay: Google\"\n" +
            "    ]\n" +
            "}"); // Here there is a JSONArray for companies property

    Map<String, Object> obj = new HashMap();
    List<String> companies = new ArrayList<>();
    companies.add("a");
    companies.add("b");
    companies.add("c");
    obj.put("Name", "toto");
    obj.put("Author", "titi");
    obj.put("Company List", companies);
    JSONObject object2 = new JSONObject(obj); // here you have an ArrayList for companies property

    String name = (String) object.get("Name");
    System.out.println(name);

    String age = (String) object.get("Author");
    System.out.println(age);

    //loop array
    List msg = (List) object.get("Company List");
    Iterator<String> iterator = msg.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }

Upvotes: 3

Thomas
Thomas

Reputation: 88737

Json-simple (which you seem to be using although the version is missing so my information might be outdated) normally creates an instance of JSONArray (which extends ArrayList btw.) for arrays in json strings, i.e. your "Company List" field.

However, you are also able to pass in a different ContainerFactory which might create a different List instance for arrays so that might be the reason your cast doesn't work (again there's too little information in your question to further comment on that).

Finally, since you should get a List anyways it should be safe to cast to that:

List<?> msg = (List<?>) object.get("Company List"); 

Upvotes: 1

Murat Karag&#246;z
Murat Karag&#246;z

Reputation: 37604

You are using org.json.simple which has a different interface.

Here is how you iterate over the ArrayList

 for(String s : object.get("Company List"))
      System.out.println(s);    

Upvotes: 0

Chetan Jadhav CD
Chetan Jadhav CD

Reputation: 1146

Try JSONArray msg = object.getJSONArray("Company List"); instead of JSONArray msg = (JSONArray) object.get("Company List");

Upvotes: 0

Related Questions