Solution of two-layer nested JSON packets

Source: Internet
Author: User

Due to the background of metamorphosis, sometimes there will be two layers or even multi-layered nested JSON package.

The first layer of the good solution, and I have seen some of the larger website News the JSON package returned by the interface is just a layer.

For example, the package shown on the layer

The code is simple and intuitive.

Dict = [data objectfromjsondata];//This could be a conversion failure, and then it needs to be judged._dinnarray=[[Nsmutablearray alloc] init]; _dataarray= [Dict Objectforkey:@"Data"];  for(Nsdictionary *dinndictinch_dataarray) {Service*servic =[Service Alloc]init]; Servic.name= [Dinndict Objectforkey:@"names"]; Servic.time= [Dinndict Objectforkey:@"Server_time"]; Servic.tel=[Dinndict objectforkey:@ "Tel"];    [_dinnarray Addobject:servic]; }

It's easy to read _dinnarray.

    Service *service = [_dinnarray objectAtIndex:indexPath.row];

Two layers of nesting, read a lot of online posts, will be wrong.

For the package as shown

The code does not emphasize the point for the time being today. Just stick it down there.

Dict =[Data objectfromjsondata]; _dataarray= [[Dict objectforkey:@"Data"]retain];  for(nsdictionary*scourdictinch_dataarray) {_listarray=[scourdict Objectforkey:@"tel_list"]; Nsmutablearray*scourarray=[[Nsmutablearray alloc]init]; //NSLog (@ "Scorearray remove%@", scourarray);         for(nsdictionary*listdictinch_listarray) {            //NSLog (@ "Listdict%@", listdict); //NSLog (@ "listdict count%d", [listdict Count]);address*address=[Address Alloc]init]; Address.namelist=[listdict Objectforkey:@"name_contacts"]; Address.phone=[listdict Objectforkey:@"Phone_number"];        [Scourarray addobject:address];    } [_fourarray Addobject:scourarray]; }

Reading the array part

    _adressarray=[_fourarray objectAtIndex:indexPath.section];    Address*address=[_adressarray ObjectAtIndex:indexPath.row];

The point is that in the existing section, the Scourarray must be a local variable, otherwise it can only store the last set of data, and the front will be overwritten.

In addition, read the part, to set two layers to read out the array. This is similar to the workaround for a two-dimensional array. A two-dimensional array is stored, and a two-dimensional array is read and read.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.