Java 用JSON填充UI

Java 用JSON填充UI,java,android,json,Java,Android,Json,我需要帮助。我正在做一些项目进行审查,我一直在用JSON填充UI 下面是JsonUtils.class的代码 package com.udacity.sandwichclub.utils; import com.udacity.sandwichclub.model.Sandwich; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.A

我需要帮助。我正在做一些项目进行审查,我一直在用JSON填充UI

下面是JsonUtils.class的代码

package com.udacity.sandwichclub.utils;

import com.udacity.sandwichclub.model.Sandwich;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class JsonUtils {

    public static Sandwich parseSandwichJson(String json) {

        try {
            //Root JSONObject Sandwich
            JSONObject jsonSandwich = new JSONObject(json);

            //Sandwich common name and also known as
            JSONObject sandwichName = jsonSandwich.getJSONObject("name");

            //Common name of the sandwich
            String commonName = sandwichName.getString("mainName");

            //Sandwich is also known as
            List<String> alsoKnownAsName = jsonArrayList(jsonSandwich
                    .getJSONArray("alsoKnownAs"));

            //Place of origin of the sandwich
            String placeOfOrigin = jsonSandwich.getString("placeOfOrigin");

            //Description of the sandwich
            String descript = jsonSandwich.getString("description");

            //Image of the sandwich
            String sandwichImg = jsonSandwich.getString("image");

            //Ingredients of the sandwich
            List<String> sandwichIngredients = jsonArrayList(jsonSandwich
                    .getJSONArray("ingredients"));

            //Return object Sandwich
            return new Sandwich(commonName, alsoKnownAsName, placeOfOrigin,
                    descript, sandwichImg, sandwichIngredients);

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

    //Method for listing ingredients and also known as names
    public static List<String> jsonArrayList(JSONArray jsonArray) throws JSONException {
        List<String> jsonList = new ArrayList<String>();

        if(jsonArray != null) {
            for(int i=0; i < jsonArray.length(); i++) {
                jsonList.add(jsonArray.getString(i));
            }
        }

        return jsonList;
    }
}
所以这种方法对我不起作用。。。当我从MainActivity中单击一个项目时,会弹出一条toast消息,表示没有相关数据

这就是Logcat所说的:

02-18 18:56:47.591 4291-4291/com.udacity.sandwichclub W/System.err: org.json.JSONException: No value for alsoKnownAs
02-18 18:56:47.591 4291-4291/com.udacity.sandwichclub W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
02-18 18:56:47.591 4291-4291/com.udacity.sandwichclub W/System.err:     at org.json.JSONObject.getJSONArray(JSONObject.java:584)
02-18 18:56:47.591 4291-4291/com.udacity.sandwichclub W/System.err:     at com.udacity.sandwichclub.utils.JsonUtils.parseSandwichJson(JsonUtils.java:28)
02-18 18:56:47.591 4291-4291/com.udacity.sandwichclub W/System.err:     at com.udacity.sandwichclub.DetailActivity.onCreate(DetailActivity.java:42)
02-18 18:56:47.591 4291-4291/com.udacity.sandwichclub W/System.err:     at android.app.Activity.performCreate(Activity.java:6662)
02-18 18:56:47.591 4291-4291/com.udacity.sandwichclub W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
02-18 18:56:47.593 4291-4291/com.udacity.sandwichclub W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
02-18 18:56:47.593 4291-4291/com.udacity.sandwichclub W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
02-18 18:56:47.593 4291-4291/com.udacity.sandwichclub W/System.err:     at android.app.ActivityThread.-wrap12(ActivityThread.java)
02-18 18:56:47.593 4291-4291/com.udacity.sandwichclub W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
02-18 18:56:47.593 4291-4291/com.udacity.sandwichclub W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
02-18 18:56:47.593 4291-4291/com.udacity.sandwichclub W/System.err:     at android.os.Looper.loop(Looper.java:154)
02-18 18:56:47.593 4291-4291/com.udacity.sandwichclub W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6077)
02-18 18:56:47.593 4291-4291/com.udacity.sandwichclub W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
02-18 18:56:47.593 4291-4291/com.udacity.sandwichclub W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
02-18 18:56:47.593 4291-4291/com.udacity.sandwichclub W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

异常表明您的json密钥不存在或不正确

检查您的密钥是否存在,然后解析它

    if(o.has("alsoKnownAs")){
         JSONArray array= o.getJSONArray("alsoKnownAs"); 
     }
为什么不使用解析json呢

只有使用这些行才能解析json字符串:

public static Sandwich parseSandwichJson(String json) {
    Gson gson = new Gson();
    Sandwich sandwich = gson.fromJson(json, Sandwich.class);
}
无论如何,您的问题是因为您尝试解析的json没有元素alsoKnownAs


使用getJSONArray方法检索强制值。如果该值是可选的,则需要使用optJSONArray。

发布日志errors@ALTegani我已经编辑了这篇文章,看一看。嗯。。。打印出您的JSON,并告诉我们是否;该不该工作。很难解析我们看不到的东西好吧,我编辑了答案。您需要使用optJSONArray方法来读取alsoKnownAs元素我发现了错误。。。aka名称列表被错误解析。。。我在根对象上解析了它,改为对象名,现在我需要在UIOk中修复一些混乱,很好。考虑一下…其他时刻的方法。检查此项
public static Sandwich parseSandwichJson(String json) {
    Gson gson = new Gson();
    Sandwich sandwich = gson.fromJson(json, Sandwich.class);
}