1

I have a JSON file as below

{
    "ClassId": "1",
    "ClassName": "mobiles",
    "ClassItems": [
    {
        "ItemId": "1",
        "ItemName": "Nokia",
        "ItemImageName": "nokia.jpg",
        "ItemUnitPrice": "200.00",
        "ItemDiscountPercent": "0" 
    },
    {
        "ItemId": "2",
        "ItemName": "Samsung",
        "ItemImageName": "samsung.jpg",
        "ItemUnitPrice": "400.00",
        "ItemDiscountPercent": "0" 
    }
    ]
}

I am trying to access the ItemIds for all the items with ClassId=1. I am able to print the complete json array but I am not able to find print itemIds alone.

The java code I use is below:

public class JSONExample {

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws IOException {
        JSONParser parser=new JSONParser();
        try {
            Object obj=parser.parse(new FileReader("JSON/TestJson.json"));
            JSONObject jsonObject=(JSONObject) obj;

            String classId=(String) jsonObject.get("ClassId");
            String className=(String) jsonObject.get("ClassName");
            if(classId.equals("1")){                

                JSONArray itemList = (JSONArray) jsonObject.get( "ClassItems" );

                Iterator iterator=itemList.iterator();

                while(iterator.hasNext())
                {   
                    System.out.println(itemList.get(1));                    
                    iterator.next();

                }
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(JSONExample.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(JSONExample.class.getName()).log(Level.SEVERE, null, ex);
        }
    }   
}

1 Answer 1

2
String itemId;
for (int i = 0; i < itemList.length(); i++) {
        JSONObject obj= itemList.getJSONObject(i);
        itemId=(String) jsonObject.get("ItemId");
}

Try the above instead of iterator. Note: if you're using(importing) org.json.simple.JSONArray, you have to use JSONArray.size() to get the data you want. But use JSONArray.length() if you're using org.json.JSONArray.

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

5 Comments

I tried as you mentioned, but received the below errors: error: cannot find symbol { System.out.println(itemList.length()); symbol: method length() location: variable itemList of type JSONArray and error: cannot find symbol JSONObject obj1= itemList.getJSONObject(i); symbol: method getJSONObject(int) location: variable itemList of type JSONArray
You just need not to copy paste the whole code but instead use it as a refrence. I might be using some other json api. But you can find similar method in your api also. Just you can get your logic from above example.
Note: if you're using(importing) org.json.simple.JSONArray, you have to use JSONArray.size() to get the data you want. But use JSONArray.length() if you're using org.json.JSONArray.
Thank you, the JSONArray.size() works perfectly for me as I am org.json.simle.JSONArray
Glad that it helped. If it worked you can choose the answer to be correct

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.