我读了许多有关通过Http请求解析json的文章,但几乎没有人回答关于解析本地json字符串的最直接方法是什么的问题。我在这里找到了一些不同的解决方案,其中Deserializing local NSString of JSON into objects via RestKit (no network download)其中一些有效,而另一些则不起作用。是否有对本地JSON字符串反序列化的任何“官方” RestKit支持?

最佳答案

幸运的是,Apple从iOS 5.0提供了“ NSJSONSerialization”类。序列化您的数据。
步骤1:将本地JSON字符串转换为“ NSData”

     NSString *strLocalJSON = @"{data= "some data"}"; //just for ex. may not be valid one
     NSData *dataJSON = [str dataUsingEncoding:NSUTF8StringEncoding];


第2步:

   id jsonobject= [NSJSONSerialization JSONObjectWithData:dataJSON options:NSJSONReadingMutableContainers error:nil]; // resulting object may contain Dictionary or Array depends on your localjson


有关更多信息,请参考https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html

10-08 05:45