用JsonObject解析JsonArray

时间:2015-08-31 11:41:30

标签: android parsing javaparser

我有一些JSON具有以下结构:

{
    "items":[
        {
            "product":{
                "product_id":"some",
                "price_id":"some",
                "price":"some",
                "title_fa":"some",
                "title_en":"Huawei Ascend Y300",
                "img":"some",
                "has_discount_from_price":"0",
                "discount_from_price":null,
                "type_discount_from_price":null,
                "has_discount_from_product":"0",
                "discount_from_product":null,
                "type_discount_from_product":null,
                "has_discount_from_category":"0",
                "discount_from_category":null,
                "type_discount_from_category":null,
                "has_discount_from_brand":"0",
                "discount_from_brand":null,
                "type_discount_from_brand":null,
                "weight":null,
                "features":[
                    {
                        "feature_value":"#000000",
                        "feature_id":"some",
                        "feature_title":"some"
                    },
                    {
                        "feature_value":"some",
                        "feature_id":"1652",
                        "feature_title":"some"
                    }
                ]
            },
            "number":1,
            "feature_id":"56491,56493",
            "price_inf":{
                "has_discount":0,
                "discount_type":0,
                "final_price":"400000",
                "value_discount":0
            },
            "cart_id":13
        }
    ]
}

我试图访问元素" product_id"和" price_id"使用以下Java代码:

try{
        JSONArray feedArray=response.getJSONArray("items");
        for (int i=0;i<feedArray.length();i++){
            JSONObject feedObj=feedArray.getJSONObject(i);

            JSONObject pro=feedObj.getJSONObject("product");
            Product product = new Product();
            product.setPrice(pro.getDouble("price_id"));
            product.setTitle_fa(pro.getString("price_id"));}}

但我发现找不到产品错误。我的解析器出了什么问题?

4 个答案:

答案 0 :(得分:1)

首先,您的JSON有效。所以不用担心。 现在关于你的问题,因为你还没有发布日志,所以我无法确定究竟是什么问题。但是使用此代码段可以获得所需的值。

 try {
      JSONArray itemsJsonArray = jsonObject.getJSONArray("items");
      for (int i = 0; i < itemsJsonArray.length(); i++){
            JSONObject itemJsonObject = itemsJsonArray.getJSONObject(i);
            JSONObject productObject = itemJsonObject.getJSONObject("product");
            String productId = productObject.getString("product_id");
            String priceId = productObject.getString("price_id");
      }

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

答案 1 :(得分:0)

验证并为您的json here

创建Pojo

使用

Data data = gson.fromJson(this.json, Data.class);

关注https://stackoverflow.com/a/5314988/5202007

顺便说一句,你的JSON无效。

答案 2 :(得分:0)

您从响应中获取json对象而不是json数组,您需要进行以下更改

JSONObject temp =new JSONObject(response);
JSONArray feedArray=temp.getJSONArray("items");

答案 3 :(得分:0)

尝试首先将响应字符串转换为JSONObject

try{
JSONObject temp =new JSONObject(responseString); // response is a string
JSONArray feedArray=.getJSONArray("items");
      ....
  }

您可以尝试使用GSON库来解析JSON字符串。这是一个如何使用GSON的例子,

    Gson gson = new Gson(); // Or use new GsonBuilder().create();
 MyType target = new MyType();
 String json = gson.toJson(target); // serializes target to Json
 MyType target2 = gson.fromJson(json, MyType.class); // deserializes json into target2
相关问题