I have below snippet that I am supposed to create in objective-c.
{
"expression": "???=n1+n2+n3+n4",
"terms": [
{
"name": "termsTitle",
"term": [
{
"name": "answer",
"value": "???"
}
]
}
]
}
Here's my attempt to create it
NSMutableArray *terms= [[NSMutableArray alloc] init];
NSArray *keysTerms = [[NSArray alloc] initWithObjects:#"name", #"term", nil];
NSMutableArray *term= [[NSMutableArray alloc] init];
NSArray *objectsTerms = [[NSArray alloc] initWithObjects:#"answer",term, nil];
NSDictionary *termsDict = [[NSMutableDictionary alloc] initWithObjects:objectsTerms forKeys:keysTerms];
[terms addObject:termsDict];
NSDictionary *statement = [[NSDictionary alloc]
initWithObjectsAndKeys:expression,#"expression",
terms,#"terms",
nil];
I think I am close but for some reason, this is not working for me. I would appreciate any help. Thanks in advance.
this should work
NSDictionary* termDict = [NSDictionary dictionaryWithObjects:#[#"answer",#"???"] forKeys:#[#"name",#"value"]];
NSArray* termArray = [NSArray arrayWithObjects:termDict, nil];
NSDictionary* termsDict = [NSDictionary dictionaryWithObjects:#[#"termsTitle",termArray] forKeys:#[#"name",#"term"]];
NSMutableArray* terms = [NSMutableArray arrayWithObjects:termsDict, nil];
NSDictionary* result = [NSDictionary dictionaryWithObjects:#[#"???=n1+n2+n3+n4",terms] forKeys:#[#"expression",#"terms"]];
NSLog(#"result: %#",[result description]);
The Result
2014-11-21 15:03:14.396 Answering_question[23716:113224] result: {
expression = "???=n1+n2+n3+n4";
terms = (
{
name = termsTitle;
term = (
{
name = answer;
value = "???";
}
);
}
);
}
If you have some problems with the structure you need to write more detailed code. Like this:
NSMutableDictionary *mainDict= [NSMutableDictionary new];
[mainDict setObject:#"???=n1+n2+n3+n4" forKey:#"expression"];
NSMutableArray *terms = [NSMutableArray new];
NSMutableDictionary *term = [NSMutableDictionary new];
[term setObject:#"name" forKey:#"termsTitle"];
NSMutableArray *subTerms = [NSMutableArray new];
NSMutableDictionary *subTerm = [NSMutableDictionary new];
[subTerm setObject:#"answer" forKey:#"name"];
[subTerm setObject:#"???" forKey:#"value"];
[subTerms addObject:subTerm];
[term setObject:subTerms forKey:#"term"];
[terms addObject:term];
[mainDict setObject:terms forKey:#"terms"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:mainDict
options:NSJSONWritingPrettyPrinted
error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
Output:
{
"expression" : "???=n1+n2+n3+n4",
"terms" : [
{
"termsTitle" : "name",
"term" : [
{
"name" : "answer",
"value" : "???"
}
]
}
]
}
Related
I'm trying to create JSON that looks like this:
{
"id": "feed/http://feeds.feedburner.com/design-milk",
"title": "Design Milk",
"categories": [
{
"id": "user/category/test",
"label": "test"
}
]
}
I'm doing it with this method:
NSMutableDictionary *req = [NSMutableDictionary #"feed/http://feeds.feedburner.com/design-milk" forKey:#"id"];
[req #"Design Milk" forKey:#"title"];
NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys:
#"user/category/test", #"id", #"test", #"label",
nil];
[req setObject:tmp forKey:#"categories"];
NSData *postdata = [NSJSONSerialization dataWithJSONObject:req options:0 error:&error];
However, this isn't working. What am I doing wrong here?
The first line of your code isn't going to compile, so you need to fix that.
The value of "categories" in your example output is an array with one element, which happens to be a dictionary. So you need
NSDictionary* oneCategory = [NSDictionary dictionaryWithObjectsAndKeys:...];
NSArray* categories = [NSArray arrayWithObject:oneCategory];
[req setObject:categories forKey:#"categories"];
And you should really use more modern syntax, like
req [#"categories"] = #[oneCategory];
You were missing an array:
NSMutableDictionary *req =[NSMutableDictionary dictionaryWithObjectsAndKeys:#"feed/http://feeds.feedburner.com/design-milk", #"id", nil];
req[#"title"] = #"Design Milk";
NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys:
#"user/category/test", #"id",
#"test", #"label",
nil];
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr addObject:tmp];
[req setObject:arr forKey:#"categories"];
NSError *error;
NSData *postdata = [NSJSONSerialization dataWithJSONObject:req options:0 error:&error];
Webservice sends me JSON that looks like that:
[
{"key":"key1","value":"value1"},
{"key":"key2","value":"value2"},
{"key":"key3","value":"value3"}
]
How should I set up RestKit mapping to get the following NSDictionary?
#{ #"key1" : #"value1", #"key2" : #"value2", #"key3": #"value3" }
This may help you, try this.
NSDictionary *dict1 = [[NSMutableDictionary alloc] init];
NSArray *arr = [[NSArray alloc] initWithArray:<Parse the array here from RESTkit>];
for (int i = 0;i < [arr count], i++)
{
NSDictionary *dict = [arr objectAtIndex:i];
NSString *key = [dict objectForKey:#"key"];
NSString *value = [dict objectForKey:#"value"];
[dict1 setObject:value forKey:key];
}
NSLog(#" Dictionary %#", dict1);
[dict1 release];
I'm trying to parse this JSOn with multiple entries of "node" tag:
{
"nodes": [
{
"node": {
"title": "Jornada del FĂștbol Profesional contra el hambre",
"description": "
"image": ",
"fecha": ,
"nid": ",
"noticia_relacionada_1_path": ,
"noticia_relacionada_2_path": ,
"image_small_2":
}
}
]
}
Here is the code:
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
//data =nil;
NSDictionary *results = [responseString JSONValue];
NSArray *noticias = [results objectForKey:#"node"];
self.noticiasArray = [[NSMutableArray alloc] init];
for (int i = 0; i < [noticias count]; i++) {
news *noticia = [[news alloc] initWithDictionary:[noticias objectAtIndex:i]];
[self.noticiasArray addObject:noticia];
noticia=nil;
}
I got 10 elements in the notices array, but when I try to get the inner values in the initWithDictionary: they are always null.
Many thanks
Example:
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
//data =nil;
NSDictionary *results = [responseString JSONValue];
NSDictionary *noticias = [results objectForKey:#"node"];
news *noticia = [[news alloc] init];
noticia.title = [noticias objectForKey:#"title"];
// do the same for the rest of the variables
[self.noticiasArray addObject:noticia];
noticia=nil;
Its only outputting one set for NSMutableDictionary not both. I want to create an JSON request using NSMutableDictionary (JSONRepresentation).
// My code
NSArray *keysEndpoint = [NSArray arrayWithObjects:#"ID", #"Name", #"EndpointType", nil];
NSArray *objectEndpoint = [NSArray arrayWithObjects:#"622", #"Brand", #"0", nil];
NSArray *keysEndpoint1 = [NSArray arrayWithObjects:#"ID", #"Name", #"EndpointType", nil];
NSArray *objectEndpoint1 = [NSArray arrayWithObjects:#"595", #"CK-05052011", #"1", nil];
NSMutableArray *keys1 = [[NSMutableArray alloc] initWithCapacity:0];
NSMutableArray *objects1 = [[NSMutableArray alloc] initWithCapacity:0];
[keys1 addObjectsFromArray:keysEndpoint];
[keys1 addObjectsFromArray:keysEndpoint1];
NSLog(#"Key Dic: %#", keys1);
[objects1 addObjectsFromArray:objectEndpoint];
[objects1 addObjectsFromArray:objectEndpoint1];
NSLog(#"Obje Dic: %#", objects1);
NSMutableDictionary *testMut = [NSMutableDictionary dictionaryWithObjects:objects1 forKeys:keys1];
NSLog(#"Test Dic: %#", testMut);
Output is am getting is this:
Test Dic: {
EndpointType = 1;
ID = 595;
Name = "CK-05052011";
}
Expexted output i want is :
Test Dic: {
EndpointType = 1;
ID = 595;
Name = "CK-05052011";
}
{
EndpointType = 0;
ID = 622;
Name = "Brand";
}
For a dictionary, adding the same keys twice will override the first set of keys. You should have a NSMutableArray of NSMutableDictionary
NSArray *keysEndpoint = [NSArray arrayWithObjects:#"ID", #"Name", #"EndpointType", nil];
NSArray *objectEndpoint = [NSArray arrayWithObjects:#"622", #"Brand", #"0", nil];
NSArray *keysEndpoint1 = [NSArray arrayWithObjects:#"ID", #"Name", #"EndpointType", nil];
NSArray *objectEndpoint1 = [NSArray arrayWithObjects:#"595", #"CK-05052011", #"1", nil];
NSMutableDictionary *testMut = [NSMutableDictionary dictionaryWithObjects:objectsEndpoint forKeys:keysEndpoint];
NSMutableDictionary *testMut1 = [NSMutableDictionary dictionaryWithObjects:objectsEndpoint1 forKeys:keysEndpoint1];
NSMutableArray * dictArray = [NSMutableArray arrayWithObjects:testMut,testMut1,nil];
NSLog(#"Test DictArray: %#", dictArray);
I just want to parse this JSON string in Objective-C using the SBJSON framework, and retrieve the three units of data:
{"x":"197","y":"191","text":"this is a string"}
How can this be done?
NSString * jsonString = #"{\"x\":\"197\",\"y\":\"191\",\"text\":\"this is a string\"}";
SBJSON *jsonParser = [[SBJSON alloc] init];
NSDictionary * dictionary = [jsonParser objectWithString:jsonString];
NSLog(#"x is %#",[dictionary objectForKey:#"x"]);
[jsonParser release];
Here's an example:
NSString *jsonText = #"...";
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *dict = [parser objectWithString:jsonText];
for (NSString *key in [#"x y text" componentsSeparatedByString:#" "]) {
NSLog(#"%# => %#", key, [dict objectForKey]);
}
Here's something similar for SBJson4Parser:
id parser = [SBJson4Parser parserWithBlock:^(id v, BOOL *stop) {
for (NSString *key in [#"x y text" componentsSeparatedByString:#" "]) {
NSLog(#"%# => %#", key, [v objectForKey]);
}
}
allowMultiRoot:NO
unwrapRootArray:NO
errorHandler:^(NSError *err) {
// handle error here
}];
NSString *jsonText = #"...";
[parser parse: [jsonText UTF8String]];