ios - Why SwiftyJSON cannot parse Array String in swift 3 -
{ "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 }
i cannot parse telephone
value above json object following code.
for item in swiftyjsonvar["item"].array! { if let jsondict = item.dictionary { let pid = jsondict["pid"]!.stringvalue let productsname = jsondict["productsname"]!.stringvalue var telephones = [string]() telephone in (jsondict["telephone"]?.array)! { telephones.append(telephone.stringvalue) } } }
i want , display 1 one phone number of above json. i'm not sure why above code not working. please me how solve it, thanks.
because telephone
string looks array, not array itself. server encoded array terribly. need json-ify again loop through list of telephone numbers:
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) telephone in telephonejson.arrayvalue { telephones.append(telephone.stringvalue) } } }
Comments
Post a Comment