IOS development-splicing Json strings with SBJson

Source: Internet
Author: User

The SBJson package is in the previous article.

You can use the key-value pairs in NSDictionary to splice Json data, which is very convenient and can also be nested and directly run the Code:

// Start splicing the Json string NSDictionary * dataDictionary = [NSDictionary dictionaryWithObjectsAndKeys: @ "mac", @ "mac", @ "game", @ "game", @ "devicetoken ", @ "devicetoken", @ "device", @ "device", @ "gv", @ "gv", @ "lang", @ "lang", @ "OS ", @ "OS", nil]; NSDictionary * parmDictionary = [NSDictionary dictionaryWithObjectsAndKeys: @ "getSession", @ "act", dataDictionary, @ "data", nil]; NSDictionary * jsonDictionary = [NSDictionary dictionary: @ "pv", @ "pv", parmDictionary, @ "param", nil]; SBJsonWriter * writer = [[SBJsonWriter alloc] init]; NSString * jasonString = [writer stringWithObject: jsonDictionary]; NSLog (@ "% @", jasonString );

The above Code uses layer-3 nesting. Note that in NSDictionary, values are preceded by keys.

The result after splicing is as follows:

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

After parsing using the Json online verification tool:

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


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.