如何从android中的url解析Json数据?这种类型

时间:2013-12-19 07:37:41

标签: android json parsing url

我跟着this来解析JSON表单网址

我要从网址解析json数据我几乎完成了解析数据但是在2或3个字段中进行了攻击

像这样

{
"returnCode": "success",
"ProductData": {
"results": [
  {
    "details": [
      {
        "latestoffers": [

        ],
        "offers_count": 0,
        "recentoffers_count": 0,
        "sku": "300",
        "url": "google.com"
      },
      {
        "latestoffers": [
          {
            "id": "5",
            "price": "311.95",
            "availability": "Available",
            "currency": "USD"
          }
        ],
        "offers_count": 1,
        "name": "b",
        "recentoffers_count": 1,
        "sku": "1",
        "url": "google.com"
      },
      {
        "latestoffers": [
          {
            "id": "2",
            "shipping": "8",
            "price": "299",
           }
        ],
        "sku": "856",
        "url": "google.com"
      }
    ],
.
    "gtins": [
      "58"
    ],
    .
    .
    .

    "features": {
    },
    "geo": [
      "usa"
    ],

    "images": [
      "google.com"
    ],

  }
],
"total_results_count": 1,
"results_count": 1,
"code": "OK",
 }
}

我解析了一些字段。(点)我看到“详细信息”最新的图片和图片我不知道如何解析它们我浪费了很多时间......

给我任何建议如何解决上述三个细节,最新图片,功能,图片..

我解析了大量数据,差不多60%,但无法解析一些数组和对象..在android中。

2 个答案:

答案 0 :(得分:0)

String jsonData= /*put your JsonData here*/
JSONObject objJson = new JSONObject(jsonData);

and String returnCode= objJson.getString("returnCode");

JSONArray ProductData= new JSONArray(objJson.getString("ProductData"));

您可以在json中访问您的数据。

如果json在数据中有另一个json,你可以使用以下行;

JSONObject innerObjJson = objJson.getJSONObject("InnerObjJsonName"); 

答案 1 :(得分:0)

试试这个..

JSONObject objJson = new JSONObject(response);
String returnCode= objJson.getString("returnCode");
JSONObject objpro = objJson.getJSONObject("ProductData");
JSONArray arrresults = objpro.getJSONArray("results");

for(int i = 0;i<arrresults.length();i++){
    JSONObject obj = arrresults.getJSONObject(i);
    String url= obj.getString("url");
    JSONArray arrdeta = obj.getJSONArray("details");
    for(int k = 0;k<arrdeta.length();k++){
        JSONObject ObjJ = arrresults.getJSONObject(k);
        JSONArray arrlatest = ObjJ.getJSONArray("latestoffers");

        for(int j = 0;j<arrlatest.length();j++){
            JSONObject Jobj = arrlatest.getJSONObject(j);
            String currency= Jobj.getString("currency");
        }
    }
    JSONObject objfeatures = obj.getJSONObject("features");
     //Something inside features
    String some = arrimg.getString("some");
    JSONArray arrimg = obj.getJSONArray("images");
    for(int l = 0;l<arrimg.length();l++){
        String ima = arrimg.getString(l);
    }
}