2015-10-15 176 views
-1

我試圖從RESTAPI與此代碼解析JSON:解析JSON從REST API

if var dict : NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as? NSDictionary { 
    println("Dictionary: \(dict)") 
} else { 
    println("nil") 
    var resultString = NSString(data: data, encoding: NSUTF8StringEncoding) 
    println(resultString) 
} 

但詞典不創建(我認爲這不是一個有效的JSON格式),我只收到像這樣的字符串:

Optional(set_metadata('allergy',[{"id":"396","shortDescription":"Dairy-Free","longDescription":"Dairy-Free","searchValue":"396^Dairy-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"397","shortDescription":"Egg-Free","longDescription":"Egg-Free","searchValue":"397^Egg-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"393","shortDescription":"Gluten-Free","longDescription":"Gluten-Free","searchValue":"393^Gluten-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"394","shortDescription":"Peanut-Free","longDescription":"Peanut-Free","searchValue":"394^Peanut-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"398","shortDescription":"Seafood-Free","longDescription":"Seafood-Free","searchValue":"398^Seafood-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"399","shortDescription":"Sesame-Free","longDescription":"Sesame-Free","searchValue":"399^Sesame-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"400","shortDescription":"Soy-Free","longDescription":"Soy-Free","searchValue":"400^Soy-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"401","shortDescription":"Sulfite-Free","longDescription":"Sulfite-Free","searchValue":"401^Sulfite-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"395","shortDescription":"Tree Nut-Free","longDescription":"Tree Nut-Free","searchValue":"395^Tree Nut-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"392","shortDescription":"Wheat-Free","longDescription":"Wheat-Free","searchValue":"392^Wheat-Free","type":"allergy","localesAvailableIn":["en-US"]}]);)

如何將其轉換爲NSDictionary?或者我可以用這個處理一些結構?

+0

有可能是在JSON的自始無效字符。嘗試切斷它們,而不是執行JSON序列化。 – dirtydanee

回答

0

在響應的開始和結束處有無效字符。

我建議如下解決這個問題:

轉換的響應到的NSString。 不是從一開始和最後3個字符切斷「set_metadata(‘過敏’」

然後,你應該與以下字符串結束:

[{"id":"396","shortDescription":"Dairy-Free","longDescription":"Dairy-Free","searchValue":"396^Dairy-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"397","shortDescription":"Egg-Free","longDescription":"Egg-Free","searchValue":"397^Egg-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"393","shortDescription":"Gluten-Free","longDescription":"Gluten-Free","searchValue":"393^Gluten-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"394","shortDescription":"Peanut-Free","longDescription":"Peanut-Free","searchValue":"394^Peanut-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"398","shortDescription":"Seafood-Free","longDescription":"Seafood-Free","searchValue":"398^Seafood-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"399","shortDescription":"Sesame-Free","longDescription":"Sesame-Free","searchValue":"399^Sesame-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"400","shortDescription":"Soy-Free","longDescription":"Soy-Free","searchValue":"400^Soy-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"401","shortDescription":"Sulfite-Free","longDescription":"Sulfite-Free","searchValue":"401^Sulfite-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"395","shortDescription":"Tree Nut-Free","longDescription":"Tree Nut-Free","searchValue":"395^Tree Nut-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"392","shortDescription":"Wheat-Free","longDescription":"Wheat-Free","searchValue":"392^Wheat-Free","type":"allergy","localesAvailableIn":["en-US"]}] 

現在可以轉換的字符串返回的NSData :

let serializeableData = dataString!.dataUsingEncoding(NSUTF8StringEncoding) 

和JSON:

var jsonErrorOptional : NSError? 
let json: AnyObject! 
      do { 
       json = try NSJSONSerialization.JSONObjectWithData(serializeableData!, options: NSJSONReadingOptions(rawValue: 2)) 
      } catch let error as NSError { 
       jsonErrorOptional = error 
       json = nil 
      } catch { 
       fatalError() 
      } 

檢查的JSON是有效

if let _json = json as? [[String : AnyObject]] { 
    // _json should be a valid swift dictionary 
} 

我知道這似乎有點模糊的解決方案,但它

+0

好吧,我這樣做了,但是我怎樣才能轉換回JSON?並不是更好地直接轉換爲NSDictionary? –

+0

請參閱我的編輯。爲什麼要將它轉換爲NSDictionary,當你在做快捷? – dirtydanee

+0

我想用這個JSON將NSDictionary存儲到對象列表中,所以,我沒有使用你的解決方案,但是這對我來說很好:let data1 = resultString!.dataUsingEncoding(NSUTF8StringEncoding) var json:AnyObject? = NSJSONSerialization.JSONObjectWithData(data1 !,選項:.MutableContainers,錯誤:無) –

1

前工作對我來說,你發佈的字符串是從控制檯日誌語句。這是結:

Optional(set_metadata('allergy',[{"id":"396","shortDescription":"Dairy-Free","longDescription":"Dairy-Free","searchValue":"396^Dairy-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"397","shortDescription":"Egg-Free","longDescription":"Egg-Free","searchValue":"397^Egg-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"393","shortDescription":"Gluten-Free","longDescription":"Gluten-Free","searchValue":"393^Gluten-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"394","shortDescription":"Peanut-Free","longDescription":"Peanut-Free","searchValue":"394^Peanut-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"398","shortDescription":"Seafood-Free","longDescription":"Seafood-Free","searchValue":"398^Seafood-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"399","shortDescription":"Sesame-Free","longDescription":"Sesame-Free","searchValue":"399^Sesame-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"400","shortDescription":"Soy-Free","longDescription":"Soy-Free","searchValue":"400^Soy-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"401","shortDescription":"Sulfite-Free","longDescription":"Sulfite-Free","searchValue":"401^Sulfite-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"395","shortDescription":"Tree Nut-Free","longDescription":"Tree Nut-Free","searchValue":"395^Tree Nut-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"392","shortDescription":"Wheat-Free","longDescription":"Wheat-Free","searchValue":"392^Wheat-Free","type":"allergy","localesAvailableIn":["en-US"]}]);) 

你的字符串是可選的,所以實際的字符串是這樣的:

set_metadata('allergy',[{"id":"396","shortDescription":"Dairy-Free","longDescription":"Dairy-Free","searchValue":"396^Dairy-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"397","shortDescription":"Egg-Free","longDescription":"Egg-Free","searchValue":"397^Egg-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"393","shortDescription":"Gluten-Free","longDescription":"Gluten-Free","searchValue":"393^Gluten-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"394","shortDescription":"Peanut-Free","longDescription":"Peanut-Free","searchValue":"394^Peanut-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"398","shortDescription":"Seafood-Free","longDescription":"Seafood-Free","searchValue":"398^Seafood-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"399","shortDescription":"Sesame-Free","longDescription":"Sesame-Free","searchValue":"399^Sesame-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"400","shortDescription":"Soy-Free","longDescription":"Soy-Free","searchValue":"400^Soy-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"401","shortDescription":"Sulfite-Free","longDescription":"Sulfite-Free","searchValue":"401^Sulfite-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"395","shortDescription":"Tree Nut-Free","longDescription":"Tree Nut-Free","searchValue":"395^Tree Nut-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"392","shortDescription":"Wheat-Free","longDescription":"Wheat-Free","searchValue":"392^Wheat-Free","type":"allergy","localesAvailableIn":["en-US"]}]); 

這不是有效的JSON。 JSON實際上從第一個[開始。以下是你需要解析JSON:

[{"id":"396","shortDescription":"Dairy-Free","longDescription":"Dairy-Free","searchValue":"396^Dairy-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"397","shortDescription":"Egg-Free","longDescription":"Egg-Free","searchValue":"397^Egg-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"393","shortDescription":"Gluten-Free","longDescription":"Gluten-Free","searchValue":"393^Gluten-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"394","shortDescription":"Peanut-Free","longDescription":"Peanut-Free","searchValue":"394^Peanut-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"398","shortDescription":"Seafood-Free","longDescription":"Seafood-Free","searchValue":"398^Seafood-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"399","shortDescription":"Sesame-Free","longDescription":"Sesame-Free","searchValue":"399^Sesame-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"400","shortDescription":"Soy-Free","longDescription":"Soy-Free","searchValue":"400^Soy-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"401","shortDescription":"Sulfite-Free","longDescription":"Sulfite-Free","searchValue":"401^Sulfite-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"395","shortDescription":"Tree Nut-Free","longDescription":"Tree Nut-Free","searchValue":"395^Tree Nut-Free","type":"allergy","localesAvailableIn":["en-US"]},{"id":"392","shortDescription":"Wheat-Free","longDescription":"Wheat-Free","searchValue":"392^Wheat-Free","type":"allergy","localesAvailableIn":["en-US"]}] 

相同的字符串漂亮的印刷:

[ 
    { 
    "id": "396", 
    "shortDescription": "Dairy-Free", 
    "longDescription": "Dairy-Free", 
    "searchValue": "396^Dairy-Free", 
    "type": "allergy", 
    "localesAvailableIn": [ 
     "en-US" 
    ] 
    }, 
    { 
    "id": "397", 
    "shortDescription": "Egg-Free", 
    "longDescription": "Egg-Free", 
    "searchValue": "397^Egg-Free", 
    "type": "allergy", 
    "localesAvailableIn": [ 
     "en-US" 
    ] 
    }, 
    { 
    "id": "393", 
    "shortDescription": "Gluten-Free", 
    "longDescription": "Gluten-Free", 
    "searchValue": "393^Gluten-Free", 
    "type": "allergy", 
    "localesAvailableIn": [ 
     "en-US" 
    ] 
    }, 
    { 
    "id": "394", 
    "shortDescription": "Peanut-Free", 
    "longDescription": "Peanut-Free", 
    "searchValue": "394^Peanut-Free", 
    "type": "allergy", 
    "localesAvailableIn": [ 
     "en-US" 
    ] 
    }, 
    { 
    "id": "398", 
    "shortDescription": "Seafood-Free", 
    "longDescription": "Seafood-Free", 
    "searchValue": "398^Seafood-Free", 
    "type": "allergy", 
    "localesAvailableIn": [ 
     "en-US" 
    ] 
    }, 
    { 
    "id": "399", 
    "shortDescription": "Sesame-Free", 
    "longDescription": "Sesame-Free", 
    "searchValue": "399^Sesame-Free", 
    "type": "allergy", 
    "localesAvailableIn": [ 
     "en-US" 
    ] 
    }, 
    { 
    "id": "400", 
    "shortDescription": "Soy-Free", 
    "longDescription": "Soy-Free", 
    "searchValue": "400^Soy-Free", 
    "type": "allergy", 
    "localesAvailableIn": [ 
     "en-US" 
    ] 
    }, 
    { 
    "id": "401", 
    "shortDescription": "Sulfite-Free", 
    "longDescription": "Sulfite-Free", 
    "searchValue": "401^Sulfite-Free", 
    "type": "allergy", 
    "localesAvailableIn": [ 
     "en-US" 
    ] 
    }, 
    { 
    "id": "395", 
    "shortDescription": "Tree Nut-Free", 
    "longDescription": "Tree Nut-Free", 
    "searchValue": "395^Tree Nut-Free", 
    "type": "allergy", 
    "localesAvailableIn": [ 
     "en-US" 
    ] 
    }, 
    { 
    "id": "392", 
    "shortDescription": "Wheat-Free", 
    "longDescription": "Wheat-Free", 
    "searchValue": "392^Wheat-Free", 
    "type": "allergy", 
    "localesAvailableIn": [ 
     "en-US" 
    ] 
    } 
]