Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to splice json strings in ios
How to splice json strings in ios
You can use key-value pairs in NSDictionary to splice Json data, which is very convenient, or you can nest them and code them directly:

//Start splicing Json strings

ns dictionary * data dictionary =[ns dictionary dictionaryWithObjectsAndKeys:@ " MAC " ,@ " MAC ",

@ "Game", @ "Game",

@ "device token", @ "device token",

@ "device", @ "device",

@“gv”,@“gv”,

"Lang", "Lang",

@"os",@"os ",nil];

ns dictionary * parm dictionary =[ns dictionary dictionaryWithObjectsAndKeys:@ " getSession " ,@ " act ",

dataDictionary,@"data ",nil];

ns dictionary * jsonDictionary =[ns dictionary dictionaryWithObjectsAndKeys:@ " PV " ,@ " PV ",

parmDictionary,@"param ",nil];

SBJsonWriter * writer =[[SBJsonWriter alloc]init];

ns string * jasonString =[writer string with object:jsonDictionary];

NSLog(@“% @”,Jason string);

The code above uses three levels of nesting. Note that in NSDictionary, the value comes before and the key comes after.

The results after stitching are as follows:

{"pv":"pv "," param":{"act":"getSession "," data":{"os":"os "," mac":"mac "," game":"game "," gv":"gv "," lang":"lang "," devicetoken":"devicetoken "," device":"device"}}}

After parsing with Json online verification tool:

{

“PV”:“PV”,

" param": {

" act": "getSession ",

"Data": {

“OS”:“OS”,

“MAC”:“MAC”,

Game: game,

Lingtai: Lingtai,

Lang: Lang,

"device token": "device token",

"equipment": "equipment"

}

}

}