即使值存在,json显示错误也没有总值

时间:2018-06-06 21:53:32

标签: android json

记录显示以下错误的猫。

  

06-07 03:12:49.829 29544-29544 / com.example.aamer.aurangabad W / System.err:org.json.JSONException:总值没有           at org.json.JSONObject.get(JSONObject.java:389)           在org.json.JSONObject.getString(JSONObject.java:550)           在com.example.aamer.aurangabad.BusinessProfile $ 4.onResponse(BusinessProfile.java:162)           在com.example.aamer.aurangabad.BusinessProfile $ 4.onResponse(BusinessProfile.java:142)           在com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:78)           在com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)           在com.android.volley.ExecutorDelivery $ ResponseDeliveryRunnable.run(ExecutorDelivery.java:106)           在android.os.Handler.handleCallback(Handler.java:761)           在android.os.Handler.dispatchMessage(Handler.java:98)           在android.os.Looper.loop(Looper.java:156)           在android.app.ActivityThread.main(ActivityThread.java:6523)           at java.lang.reflect.Method.invoke(Native Method)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:942)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)   06-07 03:13:51.675 29544-29544 / com.example.aamer.aurangabad W / IInputConnectionWrapper:非活动InputConnection上的getExtractedText   06-07 03:13:51.685 29544-29544 / com.example.aamer.aurangabad W / IInputConnectionWrapper:getTextBeforeCursor on inactive InputConnection

,这是解码json的代码。

try {

         JSONObject obj = new JSONObject(response);
         JSONArray jsonArray = obj.getJSONArray("result");

         JSONObject json_obj = jsonArray.getJSONObject(0);

          name.setText(json_obj.getString("business_name"));

          one_mobile.setText(json_obj.getString("mobile_one"));


           two_mobile.setText(json_obj.getString("mobile_two"));
           email.setText(json_obj.getString("email_id"));
           address.setText(json_obj.getString("address"));

           working_hours.setText(json_obj.getString("working_hour"));
           phone = json_obj.getString("mobile_one");

            businessrating.setText(json_obj.getString("total"));

            Glide.with(BusinessProfile.this)               .load("http://ansofexpert.com/dashboard/uploads/"+json_obj.getString("feature_image"))
                 .thumbnail(0.1f)
                 .dontAnimate()
                 .centerCrop()
                 .into(feature_image);


    } catch (JSONException e) {
       e.printStackTrace();
   }

这是来自服务器的json响应

  

{"导致":[{" business_id":" 1&#34 ;,   " business_name":"中国大陆餐厅",   " mobile_one":" 2147483647&#34 ;,   " mobile_two":" 2147483647&#34 ;,   " EMAIL_ID":" ashishingles@rediffmail.com" ;,   "地址":" Daultabad T Point,Daultabad Road,Sharnapur,Aurangabad - MS - 431001,Maharashtra,India"," area_id":" 9&# 34 ;,   " CITY_NAME":"奥兰加巴德&#34 ;,   " establish_date":" 17-05-2018&#34 ;,   " about_business":"占地超过4英亩的风景优美的乡村风格。",   " working_hour":"上午10:00 - 晚上10:00",   " CATEGORY_ID":" 38&#34 ;,   " subcategory_id":" 2&#34 ;,   " feature_image":" 581e9e09692a4a6e5837b5bd89bc2877.jpg&#34 ;,   " register_date":" 2018年9月5日&#34 ;,   " EXPIRE_DATE":" 22-05-2018""状态":" 0"},   {"总":" 3.4"}]}

2 个答案:

答案 0 :(得分:1)

json_obj不包含"总计"在你的json字符串中。 请替换以下行:

json_obj.getString("total")

json_obj = jsonArray.getJSONObject(1);
json_obj.getString("total")

再试一次

答案 1 :(得分:1)

如果您格式化JSON,您将得到答案。参见

allprojects {
    repositories {
        google()
        jcenter()
        maven {url "https://maven.google.com"}
     } 
}

您可能需要这样做:

{
  "result":[
    {
        "business_id":"1",
        "business_name":"Mainland China Restaurant",
        "mobile_one":"2147483647",
        "mobile_two":"2147483647",
        "email_id":"ashishingles@rediffmail.com",
        "address":"Daultabad T Point, Daultabad Road, Sharnapur, Aurangabad - MS - 431001, Maharashtra, India",
        "area_id":"9",
        "city_name":"aurangabad",
        "establish_date":"17-05-2018",
        "about_business":"Spread over 4 acres of beautifully landscaped are for a rustic look.",
        "working_hour":"10:00 am - 10:00 pm",
        "category_id":"38",
        "subcategory_id":"2",
        "feature_image":"581e9e09692a4a6e5837b5bd89bc2877.jpg",
        "register_date":"09-05-2018",
        "expire_date":"22-05-2018",
        "status":"0"
    },
    {
        "total":"3.4"
    }
  ]
}