【问题标题】:How to use get data using json object and display in listivew?如何使用json对象获取数据并在listview中显示?
【发布时间】:2015-09-10 02:51:39
【问题描述】:

我有这样的 JSON

{

    "data": 

[

{

    "id": 1,
    "Name": "Choc Cake",
    "Image": "1.jpg",
    "Category": "Meal",
    "Method": "",
    "Ingredients": 

[

{

    "name": "1 Cup Ice"

},

        {
            "name": "1 Bag Beans"
        }
    ]

},
{

    "id": 2,
    "Name": "Ice Cake",
    "Image": "dfdsfdsfsdfdfdsf.jpg",
    "Category": "Meal",
    "Method": "",
    "Ingredients": 

[

                {
                    "name": "1 Cup Ice"
                }
            ]
        }
    ]

}

我正在使用 JSON 对象对数据进行反序列化

这就是我想要的

JSONObject jsonObj = new JSONObject(jsonStr);
String first = jsonObj.getJSONObject("data").getString("name");
System.out.println(first);

但是一个 Cant 似乎得到了名字或任何东西 不知道我做错了什么?

然后我试图将它显示到列表视图中,但还没有到那部分

【问题讨论】:

    标签: java android json eclipse


    【解决方案1】:

    数据是 JSON 数组,而不是 JSONObject 尝试:jsonObj.getJSONArray("data").getJSONObject(0).getString("name") 还要注意 getString 和 optString 之间的区别,如果您不希望 null 出现异常,请使用后者。

    【讨论】:

    • 好的,这行得通,现在我将如何循环它以在列表视图中显示这是我现在拥有的:JSONObject jsonObj = new JSONObject(jsonStr); int length = jsonObj .length(); for(int i=0; i<length; i++){ Toast.makeText(this, jsonObj .getJSONArray("data").getJSONObject(i).getString("Name"), Toast.LENGTH_LONG).show(); }
    【解决方案2】:

    首先从下面的方法解析你的Json,

    private ArrayList<String> getStringFromJson(String jsonStr)
    {
        ArrayList<String> mNames = new ArrayList<String>();
        JSONArray array = new JSONArray(jsonStr);
        for (int i = 0; i < array.length(); i++) {
            JSONObject row = array.getJSONObject(i);
            mNames= row.getString("Name");
        }
        return mNames;
    }
    

    【讨论】:

    • 此方法将从您的 Json 中获取所有名称...!您的 JSON 是一个复杂格式的 JSON,您需要了解它的一些主要特性...!
    【解决方案3】:
    try {
         JSONObject jsonObj = new JSONObject(jsonStr);
         jsonObj.getJSONArray("data").getJSONObject(0).getString("name")
    } catch (JSONException e) {
    
    }
    

    数据是一个 json 数组。对 json 对象使用 getJsonObject。

    参考example 创建一个 ListView 并使用来自 json 对象的数据填充它的适配器。

    【讨论】:

      【解决方案4】:

      使用 GSON 代替 JSON。希望对你有帮助。

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();
          List<Data> datas= new ArrayList<Data>();
          datas= Arrays.asList(gson.fromJson(jsonString, Data[].class));
      
          public class Ingredients {
              public String getName() {
                  return name;
              }
      
              public void setName(String name) {
                  this.name = name;
              }
      
              private String name;
          }
      
      
      
      
      public class Data {
      
          private int id;
          private String Name;
          private String Image;
          private String Category;
          private String Method;
      
          public List<Ingredients> getIngredients() {
              return Ingredients;
          }
      
          public void setIngredients(List<Ingredients> ingredients) {
              Ingredients = ingredients;
          }
      
          private List<Ingredients> Ingredients = new ArrayList<Ingredients>();
      
      
          public int getId() {
              return id;
          }
      
          public void setId(int id) {
              this.id = id;
          }
      
          public String getName() {
              return Name;
          }
      
          public void setName(String name) {
              Name = name;
          }
      
          public String getImage() {
              return Image;
          }
      
          public void setImage(String image) {
              Image = image;
          }
      
          public String getCategory() {
              return Category;
          }
      
          public void setCategory(String category) {
              Category = category;
          }
      
          public String getMethod() {
              return Method;
          }
      
          public void setMethod(String method) {
              Method = method;
          }
      
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 2014-04-12
        • 1970-01-01
        • 2015-09-10
        • 2017-05-13
        • 1970-01-01
        • 1970-01-01
        • 2022-06-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多