0

I'm doing a json parser for java. I receive the json as string and then i try to get all the keys-value

Here is my Json string

 { "Message":{"field": [ {"bit":2,"name":"AAA"}, {"bit":3,"name":"BBB"}]}}

And here is my parser:

                JSONObject jObject = new JSONObject(result); //result contains the json                 
                JSONArray info = jObject.getJSONArray("field");
                for (int i = 0 ; i < info.length(); i++) {
                    JSONObject obj = info.getJSONObject(i);
                    Iterator<String> keys = obj.keys();
                    while (keys.hasNext()) { //I use key - value cause the json can change
                        String key =  keys.next();
                        System.out.println("Key: " + key + "\tValue: " + obj.get(key));
                    }
                }

But everytime that i run the code i get:

Error parsing json org.json.JSONException: JSONObject["field"] not found.

And i guess the field its a JsonArray... Am i wrong?

Thank you for your time

3 Answers 3

2

You are one level too deep wanted to get field from your jObject. You need to do :

JSONObject jObject = new JSONObject(result);
JSONObject jMsg = jObject.getJSONObject("Message");               
JSONArray info = jMsg.getJSONArray("field");
Sign up to request clarification or add additional context in comments.

Comments

2

You need to get get JSONArray field from JSONObjct Message

String result = "{ \"Message\":{\"field\": [ {\"bit\":2,\"name\":\"AAA\"}, {\"bit\":3,\"name\":\"BBB\"}]}}";
JSONObject jObject = new JSONObject(result).getJSONObject("Message"); //result contains the json
JSONArray info = jObject.getJSONArray("field");
for (int i = 0 ; i < info.length(); i++) {
    JSONObject obj = info.getJSONObject(i);
    Iterator<String> keys = obj.keys();
    while (keys.hasNext()) { //I use key - value cause the json can change
        String key =  keys.next();
        System.out.println("Key: " + key + "\tValue: " + obj.get(key));
    }
}

Output :

Key: name   Value: AAA
Key: bit    Value: 2
Key: name   Value: BBB
Key: bit    Value: 3

Comments

0

try this:

        String result = "{ \"Message\":{\"field\": [ {\"bit\":2,\"name\":\"AAA\"}, {\"bit\":3,\"name\":\"BBB\"}]}}";

    JSONObject jObject = new JSONObject(result); //result contains the json
    JSONArray info = jObject.getJSONObject("Message").getJSONArray("field");
    for (int i = 0 ; i < info.length(); i++) {
        JSONObject obj = info.getJSONObject(i);
        Iterator<String> keys = obj.keys();
        while (keys.hasNext()) { //I use key - value cause the json can change
            String key =  keys.next();
            System.out.println("Key: " + key + "\tValue: " + obj.get(key));
        }
    }

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.