Psl
Psl

Reputation: 3920

Fetch JSONObject value from a JSON data object

I have following json data

{
    "TestMetrics": {
        "ProcessPID": "7887",
        "MemSwapped": "0",      
        "Uptime": "407",
        "webTiming": "{\"domainLookupStart\":3,\"domainLookupEnd\":67}"}
}

From this i need to fetch the webTiming json object. For that i used following code

 JSONObject launchMetricsObj = new JSONObject(jsonData);


        try {
        JSONObject objc = launchMetricsObj.getJSONObject("TestMetrics");

        JSONObject webtimingObj = objc.getJSONObject("webTiming");
        System.out.println(webtimingObj:::::: " +webtimingObj);

        } catch (JSONException e) {
        System.out.println("Exception:" + e);
        }

As the "webTiming" value is a JSONObject i tried to get it as

JSONObject webtimingObj = objc.getJSONObject("webTiming");

But I observed following Exception:

JSON Objectorg.codehaus.jettison.json.JSONException: JSONObject["webTiming"] is not a JSONObject.

Upvotes: 1

Views: 44

Answers (1)

Gal Naor
Gal Naor

Reputation: 2397

It's because webTiming is defined as a string, not JSONObject.

In order to convert it, you need to do:

JSONObject webtimingObj = new JSONObject(objc.getString("webTiming"));

Upvotes: 4

Related Questions