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