【问题标题】:How to print php response JSON response如何打印php响应JSON响应
【发布时间】:2017-10-13 04:49:12
【问题描述】:

如何在android中打印php响应json响应?

我是 android 开发人员,但我对 json 解析一无所知。

   {
      "details": [
        {
          "p_id": "19",
          "cat_id": "1",
          "p_name": "Papad",
          "p_namecode": "[01]Papad",
          "p_image": 
          "p_price": "20",
          "p_quantity": "2",
          "p_code": "25",
         "p_discount": "10",
         "p_packing": "2",
         "p_type": "small",
        "p_status": "1",
      "p": [
         {
      "q_id": "16",
      "p_id": "19",
      "q_weight": "25-gm",
      "q_price": "150",
      "q_beg": "50 bunch",
      "q_status": "1"
         },
         {
      "q_id": "17",
      "p_id": "19",
      "q_weight": "50-gm",
      "q_price": "200",
      "q_beg": "50-bunch",
      "q_status": "1"
         }
       ]
      },

    {
  "p_id": "23",
  "cat_id": "1",
  "p_name": "Palak Papad",
  "p_namecode": "[03]Palak",
  "p_image": 
  "p_price": "200",
  "p_quantity": "5",
  "p_code": "02",
  "p_discount": "15",
  "p_packing": "4",
  "p_type": "small",
  "p_status": "1",
  "p": [
    {
      "q_id": "19",
      "p_id": "23",
      "q_weight": "50- gm",
      "q_price": "15",
      "q_beg": "50 bunch",
      "q_status": "1"
    },
    {
      "q_id": "20",
      "p_id": "23",
      "q_weight": "1-kg",
      "q_price": "30",
      "q_beg": "50 bunch",
      "q_status": "1"
    }
  ]
}
],
 "success": true,
 "message": "Category and Sub Category"
}

【问题讨论】:

标签: android json


【解决方案1】:

json数据解析试试这个

String jsonResult = {JSON-RESULT-IN-STRING};
JSONObject data = new JSONObject(jsonResult);

boolean success = data.getBoolean("success");
String message = data.getString("message");

JSONArray detailArray = new JSONArray(data.getString("details"))

for (int i=0; i<detailArray.length(); i++) {
    JSONObject detail = detailArray.getJSONObject(i);
    String pID = detail.getString("p_id");
    String catID = detail.getString("cat_id");
    ....
    ....
    ....
}

【讨论】:

    【解决方案2】:

    首先,您需要了解您的 JSON 结构。您需要 GSON gradle 来轻松将您的 PHP 响应转换为 java 类对象。

    在应用级 gradle 中添加这一行。

    compile 'com.google.code.gson:gson:2.8.2'
    

    要了解您的 JSON 数据,请参见下图。它是您数据的精确表示。

    您的 JSON 数据包含一个 array of Details、一个布尔值 success 和名为 message 的字符串。但是您的 Detail 数组包含另一个名为 p 的数组。为了更好地理解,请参见下图。

    现在我们准备好了。

    创建 Java 类 P,如下所示。

    public class P {
        public int q_id;
        public int p_id;
        public String q_weight;
        public int q_price;
        public String q_beg;
        public int q_status;
    }
    

    创建新的 java 类 Detail。仔细看,创建一个完整的类。您可以参考第一张图片。

    public class Detail {
    
        public int p_id;
        public int cat_id;
        public String p_name;
        public String p_namecode;
        .
        .
        .
        public int p_status;
        public ArrayList<P> p;
    }
    

    现在我们需要一个最终类,你可以给它起任何名字。

    public class Product {
        public ArrayList<Detail> details;
        public boolean success;
        public String message;
    }
    

    现在,每当您收到 PHP 响应字符串时,您都可以像这样将其转换为 java 对象。

    Product d = new Product();
    String response = "your_php_response_goes here";
    Gson gson = new Gson();
    d = gson.fromJson(response, Product.class);
    

    您的整个响应数据都在 d 对象中。要检索数据,您可以这样做。

    String p_name = d.details.get(0).p_name;
    int p_quantity = d.deatails.get(0).p_quantity;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-20
      • 2014-11-09
      • 1970-01-01
      • 2020-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多