2017-07-26 7 views
2
{ 
    "item": [ 
     { 
      "pid": 89334, 
      "productsname": "Long Way", 
      "address": "B-4/7, Malikha Housing, Yadanar St., Bawa Myint Ward,", 
      "telephone": "[\"01570269\",\"01572271\"]" 
     }, 
     { 
      "pid": 2, 
      "productsname": "Myanmar Reliance Energy Co., Ltd. (MRE)", 
      "address": "Bldg, 2, Rm# 5, 1st Flr., Hninsi St., ", 
      "telephone": "[\"202916\",\"09-73153580\"]" 
     } 
    ], 
    "success": true 
} 

次のコードを使用して、JSONオブジェクトのtelephone値を解析できません。SwiftyJSONが迅速に配列文字列を解析できないのはなぜですか?

for item in swiftyJsonVar["item"].array! { 
    if let jsonDict = item.dictionary { 
     let pid = jsonDict["pid"]!.stringValue 
     let productsname = jsonDict["productsname"]!.stringValue 

     var telephones = [String]() 
     for telephone in (jsonDict["telephone"]?.array)! { 
      telephones.append(telephone.stringValue) 
     } 
    } 
} 

上記のJSONの電話番号を1つずつ表示したいと考えています。上記のコードがなぜ機能しないのか分かりません。私はそれを解決するために、おかげで助けてください。

答えて

4

telephoneは配列のように見える文字列であり、配列自体ではないためです。サーバーはこの配列をひどくエンコードしました。 ...

for item in swiftyJsonVar["item"].array! { 
    if let jsonDict = item.dictionary { 
     let pid = jsonDict["pid"]!.stringValue 
     let productsname = jsonDict["productsname"]!.stringValue 

     var telephones = [String]() 
     let telephoneData = jsonDict["telephone"]!.stringValue.data(using: .utf8)! 
     let telephoneJSON = JSON(data: telephoneData) 

     for telephone in telephoneJSON.arrayValue { 
      telephones.append(telephone.stringValue) 
     } 
    } 
} 
+0

優れた!!!:あなたは電話番号のリストをループに再びそれをJSON-IFYに必要 – ppshein

関連する問題