JSON Parsing in iOS 7

How to parse Json Data in iOS 7?

NSError *jsonParsingError = nil;
NSDictionary *parsedJson = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];

Now you can access json elements by using keys in the json response.
For example to get the results count.

[parsedJson objectForKey:@"resultCount"];

Update:
In your json string result is an array. So After serialization you'll get array of dictionaries. So to get a property like 'amgArtistId', first you'll have to get the value for the results key from the dictionary, which will have an array of (dictionaries). Then you can loop that array or access using index to individual dictionaries.

NSArray *results = [parsedJson objectForKey:@"results"];
NSString *amgArtistId = [[results firstObject] objectForKey:@"amgArtistId"];

This way you can get other elements as well. You don't have to serialize each and every element for that. Serializing json string once is enough.

JSON Parsing in iOS 7

The correct JSON should presumably look something like:

[
{
"id": "value",
"array": [{"id": "value"},{"id": "value"}]
},
{
"id": "value",
"array": [{"id": "value"},{"id": "value"}]
}
]

But, if you're stuck this the format provided in your question, you need to make the dictionary mutable with NSJSONReadingMutableContainers and then call NSJSONSerialization again for each of those array entries:

NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
if (error)
NSLog(@"JSONObjectWithData error: %@", error);

for (NSMutableDictionary *dictionary in array)
{
NSString *arrayString = dictionary[@"array"];
if (arrayString)
{
NSData *data = [arrayString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
dictionary[@"array"] = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (error)
NSLog(@"JSONObjectWithData for array error: %@", error);
}
}

json parsing data store in array in ios 7

You are writting dictionary to json data, instead of Reading.

// Use following lines to read JSON response.

NSDictionary *dictResult = [NSJSONSerialization JSONObjectWithData:<json data> options:NSJSONReadingAllowFragments error:nil];
NSArray *areas = [NSArray arrayWithArray:dictResult[@"Result"][@"Area"]];
NSLog(@"Areas : %@ %@",dictResult[@"Result"][@"Area"],areas);

How To Fetch and Parse JSON Using Swift 2 + XCode 7 + iOS 9

Easy way to make a request for you:

How to make an HTTP request in Swift?

Or If you want to send request by your own:

HTTP POST error Handling in Swift 2

Converting a String to JSON Object:

Just as an example, here I've converted a NSString

First of all convert the NSString to NSDictionary

static func convert(src: NSString) -> NSDictionary {
// convert String to NSData
let data = src.dataUsingEncoding(NSUTF8StringEncoding)
var error: NSError?

// convert NSData to 'AnyObject'
let anyObj: AnyObject?
do {
anyObj = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions(rawValue: 0))
} catch let error1 as NSError {
error = error1
anyObj = nil
}

if(error != nil) {
// If there is an error parsing JSON, print it to the console
print("JSON Error \(error!.localizedDescription)")
//self.showError()
return NSDictionary()
} else {

return anyObj as! NSDictionary
}

}

And then, use it like this

if  let name = dictionary["name"] as? String {
self.name = name
}

You can use the converted JSON to make an object like this

import Foundation

class Student {
var name="";

init(dictionary: NSDictionary) {
let _NAME = "name"
if let name = dictionary[_NAME] as? String {
self.name = name
}
}

}

How to json parsing in ios

You can use NSJSONSerialization class
https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/

NSDictionary *responseObject = [NSJSONSerialization
JSONObjectWithData:responseData options:self.readingOptions
error:&serializationError];


Related Topics



Leave a reply



Submit