suresh kumar
suresh kumar

Reputation: 11

How to get data from this json structure

This Structure is not supported by JSON Viewer. Can we get the data from this structure?

{
"Birthday": "[{"Name":"Gyanendra Bajpai (11198)","Date":"08 Jan 2016","EventCategory":"Birthday","EmployeeID":"145"}]"
}

Upvotes: 1

Views: 42

Answers (3)

BNK
BNK

Reputation: 24114

Your JSON is invalid, you can use some online site such as JSONLint to validate.

I suggest you change it to the following:

{
    "Birthday": {
        "Name": "Gyanendra Bajpai(11198)",
        "Date": "08 Jan 2016 ",
        "EventCategory": "Birthday",
        "EmployeeID": "145"
    }
}

Then, you can parse it as the following:

try {
    JSONObject jsonObject = new JSONObject(jsonString);
    if (!jsonObject.isNull("Birthday")) {
        JSONObject jsonObject1 = jsonObject.getJSONObject("Birthday");
        if (jsonObject1 != null) {
            String name = jsonObject1.optString("Name");
            String date = jsonObject1.optString("Date");
            String eventCategory = jsonObject1.optString("EventCategory");
            String employeeID = jsonObject1.optString("EmployeeID");
        }
    }
} catch (JSONException e) {
    e.printStackTrace();
}

Upvotes: 1

Mahalakshmi
Mahalakshmi

Reputation: 330

    your json structure is invalid change it to below structure first 
    {
        "Birthday": [{
            "Name": "Gyanendra Bajpai(11198)",
            "Date": "08 Jan 2016",
            "EventCategory": "Birthday",
            "EmployeeID": "145"
        }]
    }  
//parse json
JSONObject result = null;
result="";//pass ur response jsonobject to result param here.
JSONArray jaResBirthdayList = result.getJSONArray("Birthday");   
for (int w = 0; w < jaResBirthdayList.length(); w++) {              
    JSONObject jsonObject =jaResBirthdayList.getJSONObject(w);
    String NameStr= jsonObject.getString("Name");
    String DateStr = jsonObject.getString("Date");
    String EventCategoryStr =jsonObject.getString("EventCategory"); 
    String EmployeeIDStr =jsonObject.getString("EmployeeID");       
}

Upvotes: 1

mic4ael
mic4ael

Reputation: 8310

The problem is that you are incorrectly nesting quotation marks. You can't use " inside another " without proper escaping. So, either remove " from around [] or properly escape all the quotation marks.

Upvotes: 0

Related Questions