1

我正在尝试解析以下 json (org.simple.json)

  {
   "TotalCount":4,
   "Items":[
      {
         "ProductID":879605004504,
         "Product":{
            "Subtitle":"Vibram Fivefingers",
            "ID":879605004504,
            "Attributes":[
               {
                  "Value":"Vibram Fivefingers",
                  "Key":"Brand"
               },
               {
                  "Value":"Brown",
                  "Key":"Color"
               }
            ],
            "Media":{
               "ImageTile":"http:\/\/xxxx\/v5\/products\/879605004504\/images\/tile",

            },
            "Title":"Mens Vibram FiveFingers KSO Trek Shoes",
            "References":[
               {
                  "Name":"travelcountry.com",
                  "Link":"http:\/\/www.travelcountry.com\/shop\/vibram-fivefingers\/kso-trek-mens.html?RefId=29&RefType=Affiliate"
               },
               {
                  "Name":"summithut.com",
                  "Link":"http:\/\/www.summithut.com\/products\/mens-kso-trek\/?rc=googlebase"
               }
            ]
         },
         "ID":4,
         "CreatedAt":1346523371000000
      },
      {
         "ProductID":773040867635,
         "Product":{
            "Subtitle":"Chaco",
            "ID":773040867635,
            "Attributes":[
               {
                  "Value":"Available in Multiple Colors! - CAO1405: Credence Tall Boot by Chaco Features: -Women s Credence Tall Boot in Sienna. -Available in whole and half size. -Available in Medium width only. -The upper is constructed from full grain leather. -Part of the...",
                  "Key":"Description"
               },
               {
                  "Value":"Chaco",
                  "Key":"Manufacturer"
               },
               {
                  "Value":"Adult",
                  "Key":"Age Segment"
               },
               {
                  "Value":"Black",
                  "Key":"Color"
               },
               {
                  "Value":"Boots",
                  "Key":"Footwear Type"
               },
               {
                  "Value":"5",
                  "Key":"Size"
               }
            ],
            "Media":{
               "ImageTile":"http:\/\xxxx\/v5\/products\/773040867635\/images\/tile",

            },
            "Categories":[
               "Hidden > Shoes > Women's > Boots"
            ],
            "Title":"Chaco Credence Tall Boot - Women's"
         },
         "ID":3,
         "CreatedAt":1346523325000000
      }
   ]
}

我不知道如何解析这个 json ?它有项目的总数。谢谢。

4

1 回答 1

1

这应该让你开始,你需要继续你的定义......

JSONObject rootObject = new JSONObject(inputString);
int totalCount = rootObject.getInt("TotalCount");
JSONArray items = rootObject.getJSONArray("Items");

JSONObject item = null;
JSONObject itemDetails = null;
int productID = 0;
String subtitle = null;

for(int i = 0; i < items.size(); i++) {
    item = items.getJSONObject(i);
    productId = item.getInt()"ProductId");
    itemDetails = item.getJSONObject("Product");
    subtitle = itemDetails.getString("Subtitle");
}
于 2012-09-06T22:49:06.123 回答