-1

我有一些具有以下结构的 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
        }
    ]
}

我正在尝试使用以下 Java 代码访问元素“product_id”和“price_id”:

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

4 回答 4

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();
}
于 2015-08-31T12:32:58.837 回答
0

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

JSONObject temp =new JSONObject(response);
JSONArray feedArray=temp.getJSONArray("items");
于 2015-08-31T11:54:02.357 回答
0

在此处为您的 json 验证并创建 Pojo

利用

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

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

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

于 2015-08-31T11:51:14.720 回答
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
于 2015-08-31T12:00:50.603 回答