I am developing an application for both Android and iPhone and I am having a problem marshalling and unmarshalling on iPhone. With Android it is easy enough, I am using Jackson JSON parser and there are plenty of tutorials online which made it easy to use.
On the iPhone I am using SBJSON parser, however there seems to be a real lack of information online about how to use it effectively.
Take the following piece of JSON
{
"data":{
"name":[
{
"fName":"John",
"lName":"Doe"
},
{
"fName":"Jane",
"lName":"Doe"
}
]
}
}
If I were using Java and using Jackson JSON parser, this would be easy. I would set up a Class like
public class Parse {
private Data data;
//get set data class
Then in the data class
public class Data {
private List<Name> name;
//get set name list
then in the name class
public class Name {
private String fName;
private String lName;
//get setters here
That way I have the data split up into a set of objects so I can retrieve the data I need, I can update the JSON if required from Java, then write it out again into a new JSON file, nice and simple, i.e
ObjectMapper mapper = new ObjectMapper();
Parse test = mapper.readValue(new File("/Users/adam/Documents/JSON/list.json"),
Parse.class);
System.out.println(test.getData().getName().get(0).getfName());
or I could set it doing
test.getData().getName().get(0).setfName("test");
What I want to know, is how do I do this with Xcode using SBJSON. I know how to do parse the data, and print it out, but I want to be able to print it out into a set of objects, make a change, then write it out again as I can with Jackson JSON parser. What I have done is
NSString *file = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
NSData *Data = [NSData dataWithContentsOfFile:file];
NSString *jsonString = [[NSString alloc] initWithData:Data encoding:NSUTF8StringEncoding];
NSDictionary *dictionary = [jsonString JSONValue];
NSArray *name = [dictionary valueForKeyPath:@"data.name"];
NSLog(@"%@", name);
This will get the array of names, but I want to be able to access the first name and last name of each objects, and then update it if I require. Then Write it out again.
Would anyone be able to point me in the right direction. Is it possible to do the same sort of thing I did with Jackson JSON with SBJSON?
Found out how to do it.
Make a class Called List
Then implementation synthesise
Then code to create object of List and loop through it
Hope this helps anyone else who was trying to do the same thing I was.