无法解析JSON:No JsonObject

时间:2013-11-29 17:39:51

标签: java android json

我的Android应用程序遇到了问题。问题是我无法解析一些JSON代码,因为它没有JsonObject。我对编程很陌生,因此没有使用JSON的经验。如果这是一个新问题,请原谅我。这是我的JSON代码:

{
"item_id": "51c3d78797c3e6d8d3b546cf",
"item_name": "Cola, Cherry",
"brand_id": "51db3801176fe9790a89ae0b",
"brand_name": "Coke",
"item_description": "Cherry",
"updated_at": "2013-07-09T00:00:46.000Z",
"nf_ingredient_statement": "Carbonated Water, High Fructose Corn Syrup and/or Sucrose, Caramel Color, Phosphoric Acid, Natural Flavors, Caffeine.",
"nf_calories": 100,
"nf_calories_from_fat": 0,
"nf_total_fat": 0,
"nf_saturated_fat": null,
"nf_cholesterol": null,
"nf_sodium": 25,
"nf_total_carbohydrate": 28,
"nf_dietary_fiber": null,
"nf_sugars": 28,
"nf_protein": 0,
"nf_vitamin_a_dv": 0,
"nf_vitamin_c_dv": 0,
"nf_calcium_dv": 0,
"nf_iron_dv": 0,
"nf_servings_per_container": 6,
"nf_serving_size_qty": 8,
"nf_serving_size_unit": "fl oz",
}

这是我在Android应用中的代码。

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    EditText one = (EditText) findViewById(R.id.editText1);
    setContentView(R.layout.dbfiller);
    Intent intent = getIntent();
    final String message = intent.getStringExtra("bnum");

    DefaultHttpClient   httpclient = new DefaultHttpClient(new BasicHttpParams());
    HttpPost httppost = new HttpPost("https://api.nutritionix.com/v1_1/item?upc="+message
            +"&appId=926eed28&appKey=d1366b0005a0d8f6898ff3df44b52867");
    // Depends on your web service
    httppost.setHeader("Content-type", "application/json");

    InputStream inputStream = null;
    String result = null;
    try {
        HttpResponse response = httpclient.execute(httppost);           
        HttpEntity entity = response.getEntity();

        inputStream = entity.getContent();
        // json is UTF-8 by default
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        result = sb.toString();
        JSONObject json= (JSONObject) new JSONTokener(result).nextValue();
        JSONObject json2 = json.getJSONObject("");
        String test = (String) json2.get("nf_ingredient_statement");
        one.setText(test);
    } catch (Exception e) { 
        e.printStackTrace();        
    }
    finally {
        try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
    }
}

我认为问题在于:

            JSONObject json= (JSONObject) new JSONTokener(result).nextValue();
        JSONObject json2 = json.getJSONObject("");
        String test = (String) json2.get("nf_ingredient_statement");
        one.setText(test);

但我不知道我做错了什么。我一直在墙上敲我的头一会儿。这是我的Logcat:

11-29 12:36:13.636: W/System.err(25748): org.json.JSONException: No value for 
11-29 12:36:13.636: W/System.err(25748):    at org.json.JSONObject.get(JSONObject.java:355)
11-29 12:36:13.636: W/System.err(25748):    at org.json.JSONObject.getJSONObject(JSONObject.java:574)
11-29 12:36:13.636: W/System.err(25748):    at com.example.foodsaver2.DatabaseFiller.onCreate(DatabaseFiller.java:57)
11-29 12:36:13.636: W/System.err(25748):    at android.app.Activity.performCreate(Activity.java:5243)
11-29 12:36:13.636: W/System.err(25748):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-29 12:36:13.636: W/System.err(25748):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
11-29 12:36:13.636: W/System.err(25748):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
11-29 12:36:13.636: W/System.err(25748):    at android.app.ActivityThread.access$700(ActivityThread.java:135)
11-29 12:36:13.636: W/System.err(25748):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
11-29 12:36:13.636: W/System.err(25748):    at android.os.Handler.dispatchMessage(Handler.java:102)
11-29 12:36:13.636: W/System.err(25748):    at android.os.Looper.loop(Looper.java:137)
11-29 12:36:13.636: W/System.err(25748):    at android.app.ActivityThread.main(ActivityThread.java:4998)
11-29 12:36:13.636: W/System.err(25748):    at java.lang.reflect.Method.invokeNative(Native     Method)
11-29 12:36:13.636: W/System.err(25748):    at java.lang.reflect.Method.invoke(Method.java:515)
11-29 12:36:13.636: W/System.err(25748):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-29 12:36:13.636: W/System.err(25748):    at  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-29 12:36:13.636: W/System.err(25748):    at dalvik.system.NativeStart.main(Native Method)

应用程序不会崩溃,因为json解析器位于try-catch方法中。这有什么不对?我已经检查了Google和SO,但我似乎无法找到解决方案。非常感谢任何有关此问题的帮助。

3 个答案:

答案 0 :(得分:2)

您的JSON字符串末尾有一个逗号。关闭它,它可能会解决您的问题,因为代码看起来很好。

答案 1 :(得分:0)

您的JSON中存在错误。删除此行末尾的逗号,然后重试:

"nf_serving_size_unit": "fl oz",
}

您始终可以使用jsonlint.com处的JSON验证程序检查您的JSON是否有效。

答案 2 :(得分:0)

它有点愚蠢,但试一试:)

    String finalResult;
    finalResult= result.substring(0, result.length() -2) + "}";
    Log.i("finalResult", finalResult);