How to get all values from this Json in android?

763 Views Asked by At

I am trying to get value but not get proper only one array is for another "no values" error shown so please help me how to get value form different array. "only name array break and next for output no values found errors shown and app crashes."

{  
"project": [{

             "name": [{
             "sac": "sachin",
             "sag": "sagar"
         }]
}, {
    "output": " true",
    "msg1": [{
        "emp": "001",
        "empname": "sachin"
    }, {
        "emp": "002",
        "empname": "sagar"
    }]
}, {
    "output_prg": " true",
    "msg2": [{
        "id": "1",
        "pr_code": "SD"
    }, {
        "id": "002",
        "pr_code": "SJ"
    }]
}]
}
1

There are 1 best solutions below

0
On BEST ANSWER

You can parse the response based on index of an array If it is 0 index you can parse the data based on JSON.

Here i done json parsing for your response.

But the json response is not in standard(i.e the intention of JsonArray is not used in Project array,better to make the project JsonArray as JSONObject and assign the each index as seperate JSONObject with Json Key)

JSONArray productArray=jsonObject.getJSONArray("project");
            if(productArray.length()>0){
                JSONObject nameJson=productArray.getJSONObject(0);
                JSONArray nameJsonArray=nameJson.getJSONArray("name");
                for(int i=0;i<nameJsonArray.length();i++){
                    JSONObject nameJSonObject=nameJsonArray.getJSONObject(i);
                    String sac=nameJSonObject.getString("sac");
                    String sag=nameJSonObject.getString("sag");
                }
            }
            if(productArray.length()>1){
                JSONObject outputJSON=productArray.getJSONObject(1);
                String outputStatus=outputJSON.getString("output");
                JSONArray msgArray=outputJSON.getJSONArray("msg1");
                for(int i=0;i<msgArray.length();i++){
                    JSONObject msgJsonObject=msgArray.getJSONObject(i);
                    String empStr=msgJsonObject.getString("emp");
                    String empNameStr=msgJsonObject.getString("empname");
                }
            }
            if(productArray.length()>2){
                JSONObject outputJSON=productArray.getJSONObject(2);
                String outputPrgStatus=outputJSON.getString("output_prg");
                JSONArray msgArray=outputJSON.getJSONArray("msg2");
                for(int i=0;i<msgArray.length();i++){
                    JSONObject msgJsonObject=msgArray.getJSONObject(i);
                    String idStr=msgJsonObject.getString("id");
                    String prCodeStr=msgJsonObject.getString("pr_code");
                }
            }