-2

This is My Json String


{"Damages":[{"id":15,"rf_no":5,"state":"Print5","dmg_no":0,"town":"NEWASA","date":"16\/08\/2015","firm_name":"SHREE ENTERPRISES (NEWASE) "},{"id":36,"rf_no":7,"state":"Print7","dmg_no":0,"town":"NEWASA","date":"16\/08\/2015","firm_name":"SHREE ENTERPRISES (NEWASE) "}]}


But in Android Application its Showing No Value For Damages

This is My Android Code


JSONArray jArray = new JSONArray(json.getString("Damages"));

for (int i = 0; i < jArray.length(); i++) { JSONObject c = jArray.getJSONObject(i); final String id = c.getString("id"); String dmg_no = c.getString("dmg_no"); String firm_name = c.getString("firm_name"); final String rf_no = c.getString("rf_no"); String town=c.getString("town"); String date=c.getString("date"); String State=c.getString("state"); }

Help Me Please. And Thanx In Advance.

3 Answers3

1

You can try this code, to parse your json data:

 try
        {
            JSONObject jsonObj=new JSONObject(result);  // result=JSON string 
            if(jsonObj.has("Damages"))
            {
                JSONArray arrayObj=jsonObj.getJSONArray("Damages");
                for(int i=0;i<arrayObj.length();i++)
                {
                    JSONObject childArray=arrayObj.getJSONObject(i);
                    Log.e("", "ID "+childArray.getString("id"));
                    Log.e("", "Ref No"+childArray.getString("rf_no"));
                    // similarly you can parse rest of your tags
                }
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Hope this helps you.

A.R.
  • 2,631
  • 1
  • 19
  • 22
1

This is your mistake : JSONArray jArray = new JSONArray(json.getString("Damages"));

you are trying to get the string by name "Damages" and coverting it to JSONarray. But what you have to do is you have to convert your string to JSONObject first and then get the array named "Damages" from that json object.

Try this

     String jsonString = {"Damages":[{"id":15,"rf_no":5,"state":"Print5","dmg_no":0,"town":"NEWASA","date":"16\/08\/2015","firm_name":"SHREE ENTERPRISES (NEWASE) "},{"id":36,"rf_no":7,"state":"Print7","dmg_no":0,"town":"NEWASA","date":"16\/08\/2015","firm_name":"SHREE ENTERPRISES (NEWASE) "}]}

      try {
                JSONObject jsonObject=new JSONObject(jsonString);

              JSONArray damageArray=jsonObject.getJSONArray("Damages");

                for(int i=0;i<damageArray.length();i++)
                {
                    JSONObject obj=damageArray.getJSONObject(i);

                   String dmg_no = obj.getString("dmg_no");
                    String firm_name = obj.getString("firm_name");
                    final String rf_no = obj.getString("rf_no");
                    String town=obj.getString("town");
                    String date=obj.getString("date");
                    String State=obj.getString("state");

                }


            } catch (JSONException e) {
                e.printStackTrace();
            }
alka aswal
  • 511
  • 1
  • 7
  • 22
0

This is the problem line

JSONArray jArray = new JSONArray(json.getString("Damages"));

What are you doing is telling the program to find value of String Damages and then get array with name of that value - but there is no array called like that! Your array is already named, and the name is Damages.

To get the array you simply do

JSONArray jArray = json.getJSONArray("Damages");
  • assuming json is the main response you are getting
poss
  • 1,759
  • 1
  • 12
  • 27