IOS JSON parsing and iosjson Parsing

Source: Internet
Author: User

IOS JSON parsing and iosjson Parsing

Parse json into dic object

  • -(Void) fetchedData :( NSData *) responseData {// parse out the json dataNSError * error;
  • NSDictionary * json = [NSJSONSerialization
  • JSONObjectWithData: responseData // 1
  • Options: kNilOptions
  • Error: & error];
  • NSArray * latestLoans = [json objectForKey: @ "loans"]; // 2
  • NSLog (@ "loans: % @", latestLoans); // 3
  • }
  • Generate an object as a json string
  • // Build an info object and convert to json
  • NSDictionary * info = [NSDictionary dictionaryWithObjectsAndKeys: [loan objectForKey: @ "name"],
  • @ "Who ",
  • [(NSDictionary *) [loan objectForKey: @ "location"]
  • ObjectForKey: @ "country"],
  • @ "Where ",
  • [NSNumber numberWithFloat: outstandingAmount],
  • @ "What ",
  • Nil];
  • // Convert object to data
  • NSData * jsonData = [NSJSONSerialization dataWithJSONObject: info
  • Options: NSJSONWritingPrettyPrinted error: & error];
  • // Print out the data contents
  • JsonSummary. text = [[NSString alloc] initWithData: jsonData
  • Encoding: NSUTF8StringEncoding];
  • Add the json method to dic
  • @ InterfaceNSDictionary (JSONCategories)
  • + (NSDictionary *) dictionaryWithContentsOfJSONURLString :( NSString *) urlAddress;
  • -(NSData *) toJSON;
  • @ End
  • @ ImplementationNSDictionary (JSONCategories)
  • + (NSDictionary *) dictionaryWithContentsOfJSONURLString :( NSString *) urlAddress {
  • NSData * data = [NSData dataWithContentsOfURL: [NSURL URLWithString: urlAddress];
  • _ Autoreleasing NSError * error = nil;
  • Id result = [NSJSONSerialization JSONObjectWithData: data
  • Options: kNilOptions error: & error];
  • If (error! = Nil) returnnil;
  • Return result;
  • }
  • -(NSData *) toJSON {
  • NSError * error = nil;
  • Id result = [NSJSONSerialization dataWithJSONObject: self
  • Options: kNilOptions error: & error];
  • If (error! = Nil) returnnil;
  • Return result;
  • } @ End
  • Use a column
  • NSDictionary * myInfo = [NSDictionary dictionaryWithContentsOfJSONURLString: @ "http://www.yahoo.com/news.json"];
  • NSDictionary * information = [NSDictionary dictionaryWithObjectsAndKeys: @ "orange", @ "apple", @ "banana", @ "fig", nil];
  • NSData * json = [information toJSON];
  • Determine whether json-based
  • BOOL isTurnableToJSON = [NSJSONSerialization isValidJSONObject: object]

IOS development, how to use json files

JSON is generally a piece of data requested from the network interface. first, you need to send a request to the server, get a piece of JSON, and then parse it. two third-party open source class libraries, ASIHTTPRequest and SBJSON, are used.

NSURL * url = [NSURL URLWithString: [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

SendRequest = [ASIHTTPRequest requestWithURL: url];

[SendRequest setTimeOutSeconds: 30];

[SendRequest setDelegate: self];

[SendRequest startAsynchronous];

-(Void) requestFinished :( ASIHTTPRequest *) request

{

NSString * responseString = [request responseString];

If (responseString = nil | [responseString JSONValue] = nil ){

Return;

}

NSDictionary * responseDict = [responseString JSONValue];

Int result = [[responseDict objectForKey: @ "status"] intValue];

If (result = 1 ){

NSArray * location = [responseDict objectForKey: @ "locations"];

...............................

}

Ios development: how to retrieve the Field Values in json?

No third-party libraries have been used for a long time. Refer to the built-in classes.
NSJSONSerialization
Id result = [NSJSerialization JSONObjectWithData: jsonData
Options: NSJSONReadingMutableLeaves
Error: & error];

Reference: blog.csdn.net/..888559

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.