0

I need to parse a json on android like:

{"header": {
        "param": "param"
    },
    "body": {
        "param": "1",
        "param2": "2",
        "param3": [
            {
                "param4": "4",
                "param5": "5"
            },
            {
                "param6": "6"
            }
        ],
        "param7": "7",
        "param12": [
            {
                "param8": "8",
                "param13": [
                    {
                        "param9": "9"
                    },
                    {
                        "param10": "10"
                    }
                ]
            }
        ],
        "param11": "11"
    }
}

My code is:

org.json.simple.JSONObject LJsonObj = (org.json.simple.JSONObject) new JSONParser().parse(AString);
try {
    JSONArray LBody = (JSONArray) LJsonObj.get("body");
    result = LBody.toString();
    LStrTemp = result;
} catch (Exception e) {
    e.printStackTrace();
} finally {
    LJsonObj.clear();
}

return result;

It breaks or does not work at all at the moment:

JSONArray LBody = (JSONArray) LJsonObj.get ("body"); 

How can I parse the body tag?

Where is the error, tell me?

String, Integer, and other types are parsed without problems by this principle. And with json there are more complicated problems

jhamon
  • 3,603
  • 4
  • 26
  • 37
ApostolCo
  • 3
  • 1
  • Have a look at this answer, it might be related: [How to convert JSONObjects to JSONArray](https://stackoverflow.com/a/22687862/12806461) – DerFlo Jan 29 '20 at 13:51

1 Answers1

0

Without seeing the error logs it's hard to say exactly but I think it has to do with the fact that you are using a JSONArray when "body" is an object. Try JSONObject instead.

JoshuaK98
  • 381
  • 1
  • 2
  • 11