2016-03-31 98 views
0

我可以成功解析來自我的json文件的所有數據。在我收集視圖中的應用程序中,我嘗試僅將1個圖像數據解析爲單元格,但我將它與所有數據相關聯。我將和你一起分享JSON代碼和解析代碼,並最後一次模擬器的屏幕截圖。我希望你能幫助我。 謝謝如何從swift中獲取JSON的特定值

JSON代碼

{ 
    "retcode": "200", 
    "content": [{ 
     "id": 3, 
     "name": "X Treme", 
     "desc": "Polikarbon G\u00f6vde", 
     "category": "Design", 
     "thumbnail": [{ 
      "id": 2, 
      "thumbnail": "http:\/\/www.ertonga.com\/product_images\/xtreme_red.jpg" 
     }, { 
      "id": 3, 
      "thumbnail": "http:\/\/www.ertonga.com\/product_images\/xtreme_orange.jpg" 
     }, { 
      "id": 4, 
      "thumbnail": "http:\/\/www.ertonga.com\/product_images\/xtreme_blue.jpg" 
     }, { 
      "id": 5, 
      "thumbnail": "http:\/\/www.ertonga.com\/product_images\/xtreme_green.jpg" 
     }, { 
      "id": 6, 
      "thumbnail": "http:\/\/www.ertonga.com\/product_images\/xtreme_clear.jpg" 
     }, { 
      "id": 7, 
      "thumbnail": "http:\/\/www.ertonga.com\/product_images\/xtreme_grey.jpg" 
     }, { 
      "id": 8, 
      "thumbnail": "http:\/\/www.ertonga.com\/product_images\/xtreme_slred.jpg" 
     }] 
    }, { 
     "id": 4, 
     "name": "Opal", 
     "desc": "Polikarbon Sandalye", 
     "category": "Design", 
     "thumbnail": [{ 
      "id": 9, 
      "thumbnail": "http:\/\/www.ertonga.com\/product_images\/opal_orange.jpg" 
     }, { 
      "id": 10, 
      "thumbnail": "http:\/\/www.ertonga.com\/product_images\/opal_blue.jpg" 
     }, { 
      "id": 11, 
      "thumbnail": "http:\/\/www.ertonga.com\/product_images\/opal_green.jpg" 
     }] 
    }], 
    "error_msg": "" 
} 

Swift代碼

if let url = NSURL(string: urlString) { 

      if let data = try? NSData(contentsOfURL: url, options: []) 

      { 


       do { 
        let json = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) 


        var valueforname:Int = 0 

        if let blogs = json["content"] as? [[String: AnyObject]] { 


         for blog in blogs { 

          if let blog2 = blog["thumbnail"] as? [[String: AnyObject]] { 

           for blog3 in blog2 { 

            if let blog4 = blog3["thumbnail"] as? String { 

             var checkvalue1 = blog3["id"] as? Int 





             if Imagearray.contains(String(checkvalue1!)) { 


                    } 

             else { 


               Imagearray.append(blog4) 
              } 
             } 





            } 

           } 




         } 
        } 
       } 





       catch { 
        print("error serializing JSON: \(error)") 
       } 


      } 
     } 

,這裏是屏幕截圖。你可以看到椅子的不同顏色的我想只有1個顏色的每個項目

enter image description here

回答

0

試試這個:

NSURL(string: urlString) { 

    if let data = try? NSData(contentsOfURL: url, options: []) 

    { 
     do { 
      let json = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) 
      var valueforname:Int = 0 
      if let blogs = json["content"] as? [[String: AnyObject]] { 
       for blog in blogs { 
        if let blog2 = blog["thumbnail"] as? [[String: AnyObject]] { 
         if let blog4 = blog2["thumbnail"] as? String 
         { 
          Imagearray.append(blog4) 
         } 
        } 
       } 
      } 
     } 
     catch { 
      print("error serializing JSON: \(error)") 
     } 
    } 
} 
+0

謝謝回答我,但我得到了錯誤關於不能下標值類型[String:AnyObhect]],其索引類型爲'String' –

+0

ı認爲我們不能使用而不使用blog2 [thumbnail] as String –