iOS json字符串转化为NSArray(解析)

来源:互联网 发布:淘宝宝贝怎么靠前 编辑:程序博客网 时间:2024/05/21 05:21

URL:http://www.12beli.com/App/Index/Category?pid=0  往服务器发数据返回的是数组

服务器返回数据如下:

dict-123--{

    CategoryName = Komputer;

    ID = 4;

    ParentID = 0;

    child = "[{\"ID\":16,\"CategoryName\":\"Laptop\",\"ParentID\":4},{\"ID\":15,\"CategoryName\":\"Desktop\",\"ParentID\":4},{\"ID\":14,\"CategoryName\":\"Printer\",\"ParentID\":4},{\"ID\":24,\"CategoryName\":\"Aksesori Komputer\",\"ParentID\":4},{\"ID\":21,\"CategoryName\":\"Proyektor\",\"ParentID\":4},{\"ID\":204,\"CategoryName\":\"Networking\",\"ParentID\":4},{\"ID\":209,\"CategoryName\":\"Lain-Lain\",\"ParentID\":4},{\"ID\":215,\"CategoryName\":\"Hardware\",\"ParentID\":4}]";

}-


如要取child里面的内容需要将json字符串转换,否则按正常取法的话会crash

参考:

NSMutableString *jsonDataString=[NSMutableString string];

[jsonDataString setString:[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"jsonData" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil]];

NSData *nsData=[jsonDataString dataUsingEncoding:NSUTF8StringEncoding];

parsedArray=[NSJSONSerialization JSONObjectWithData:nsData options:kNilOptions error:nil];


我解析的内容 (ASIHTTPRequest)

- (void)requestFinished:(ASIHTTPRequest *)request

{

   SBJSON * json = [[SBJSONalloc]init];

    _dataArray = [jsonobjectWithString:[requestresponseString]error:nil];

   if ([_dataArraycount] !=0) {

        

       for (NSDictionary * dictin_dataArray) {

           NSString *str= [dictobjectForKey:@"child"];

            NSData *nsData=[strdataUsingEncoding:NSUTF8StringEncoding];

            NSArray *childArray=[NSJSONSerializationJSONObjectWithData:nsDataoptions:kNilOptionserror:nil];


           if ( childArray !=nil ) {

//                for(int i=0;i<[childArray count];i++)

//                {

//                    [childArray objectAtIndex:i];

//                }

               for (NSDictionary * littleDictin childArray) {

                   NSString * name = [littleDictobjectForKey:@"CategoryName"];

                  

                }

            }

         

        }

    }

}







0 0
原创粉丝点击