Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用JsonObject解析JsonArray_Android_Parsing_Javaparser - Fatal编程技术网

Android 使用JsonObject解析JsonArray

Android 使用JsonObject解析JsonArray,android,parsing,javaparser,Android,Parsing,Javaparser,我有一些JSON,其结构如下: { "items":[ { "product":{ "product_id":"some", "price_id":"some", "price":"some", "title_fa":"some", "title_en":"Huawei Ascend Y30

我有一些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"));}}
试试看{
JSONArray feedArray=response.getJSONArray(“items”);

对于(inti=0;i验证并为json创建Pojo)

使用

跟随


顺便说一下,您的JSON是无效的。

您从响应中得到的是一个JSON对象,而不是JSON数组。您需要进行以下更改

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

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

try{
JSONObject temp =new JSONObject(responseString); // response is a string
JSONArray feedArray=.getJSONArray("items");
      ....
  }
您可以尝试使用库解析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

首先,你的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();
}
试试看{
JSONArray itemsJsonArray=jsonObject.getJSONArray(“items”);
对于(int i=0;i
您的json似乎无效
调试
您的代码,并查看是否首先出现
feedArray
。@KaranMer它认为我是格式化错误,
{}
仅缺少这些。您的json无效,因为json中的顶级元素不能有任何名称。它应该是未命名的json数组
[..]
或对象
{..}
。您能给我们看一下日志文件吗?找到json异常或其他什么您需要获得产品id和价格,如下图所示。您为什么说json无效?@hrskrs已经验证了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();
}