JSONstring没有值

时间:2015-08-22 08:24:42

标签: android json

这是我的Json字符串


{"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) "}]}


但在Android应用程序中它显示没有损害赔偿的价值

这是我的Android代码

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"); }

请帮帮我。和Thanx提前。

3 个答案:

答案 0 :(得分:1)

您可以尝试使用此代码来解析您的json数据:

 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();
        }

希望这会对你有所帮助。

答案 1 :(得分:1)

这是你的错:JSONArray jArray = new JSONArray(json.getString(“Damages”));

你试图通过名称“Damages”获取字符串并将其转换为JSONarray。但是你要做的是你必须先将你的字符串转换为JSONObject,然后从那个json对象中获取名为“Damages”的数组。

试试这个

     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();
            }

答案 2 :(得分:0)

这是问题专栏

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

你在做什么告诉程序找到String Damages的值,然后获取名称为该值的数组 - 但是没有像这样的数组!您的数组已命名,名称为Damages

要获取数组,只需执行

JSONArray jArray = json.getJSONArray("Damages");
  • 假设json是您获得的主要回复