oData Objective-C add a JSon stream - iphone

I try to add a Json strem form my odata server.
It work fine with jQuery, but in objective-c ther is only xml returned.
I try this :
DataModel *proxy = [[DataModel alloc] initWithUri:#"http://www.example.com/odata/odata.svc" credential:nil];
[proxy addHeader:#"Accept" headerValue:#"application/json, text/javascript, */*; sq=0.01"];
QueryOperationResponse *response = [proxy execute:#"Example"];
NSMutableArray *array = [response getResult];
It doesn't work.
How can I do?
thanks
Gwennin

xml is what odata will return. you will need to parse the results. Here is a link to a great tutorial for doing that.
http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project

If the returned type is a Collection<> of some sort, the OData library does the work for you and converts it to an array of the correct Entity in Objective-C.
If you return a simple type, parse the result. An example is using XPath queries (although for my service, I had to remove from the returned XML a couple of unsupported root element attributes that Odata returned).
If you chose to use Xpath,I love this code: http://cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html

Related

How to map different json with RestKit?

Im new to Restkit and I want to use it for my iOS project.
I ran into a problem, because I get different JSON back from the server
and I don't know how to configure my mapping with RestKit.
Here are the two types of JSON messages I can receive from the server.
First json message:
{"result":"not found"}
Second json message:
{ "OPDRACHT":
{ "streetnaam":"value",
"postalcode":"value",
"city":"value"
}
}
I've been reading in the RestKit documentation about mapping without KVC and Dynamic Object Mapping but I can't figure out how to configure the mapping for RestKit.
Anyone an idea how to configure the mapping with RestKit in this situation?
I fixed my mapping problem today.
I changed my json output from the server in a more object structure. Now
RestKit works great with my json.

Creating Objects from JSON

I would like to be able to create an NSArray or NSDictionary with a JSON string that I receive from a web service. I am using the json framework to do this. The response string will look something along these lines:
{count:2,
data:[{"ID":8,
"Title":"Test Title",
"Author":"Test Author",
"Price":"18.00",
"Edition":"1st",
"Condition":"Good",
"UploadDate":"2012-07-28 07:25:56.0"
},
{"ID":9,
"Title":"Test Title",
"Author":"Test Author",
"Price":"18.000000000000",
"Edition":"1st",
"Condition":"Good",
"UploadDate":"2012-07-28 07:27:06.0"
}
]
}
My question is, what is the most efficient way to grab all of the data from the 'data' array and use it to create either an NSArray or NSDictionary? Any help would be much appreciated
I use RestKit for JSON parsing. It has all you need to fetch the json from a server, translate it into a dictionary, and from there into an object that you can work with. That might be more than you dared to wish for, but it works like a charm. Check out https://github.com/RestKit/RestKit for more info.
Starting from iOS5 a json parser has been integrated as a SDK native component see the following link http://www.raywenderlich.com/5492/working-with-json-in-ios-5.
You will create a NSDictionary with 2 keys "count" and "data" and then you will get the NSArray using objectForKey with the "data" key.

RestKit - Appending a parameter to all requests

I have an iOS app using RestKit in order to communicate with a RESTful Rails server. The server uses a basic session token for authentication of users. I want to append this token to every request sent using the RKObjectManager's methods.
I've tried creating a Category overload of NSManagedObject in order to use the following method:
- (void)willSendWithObjectLoader:(RKObjectLoader *)objectLoader
That works fine, however I see no way of appending to the object loader's params. I've even gone as far as to reserialize the object but there is no way to do that without it being sent using escape characters. For example, the following code will give me the object in JSON serialized form
RKObjectMapping *serializationMapping = [[[RKObjectManager sharedManager] mappingProvider] serializationMappingForClass:[self class]];
RKObjectSerializer *ser = [RKObjectSerializer serializerWithObject:self mapping:serializationMapping];
NSObject<RKRequestSerializable> *obj = [ser serializationForMIMEType:RKMIMETypeJSON error:nil];
But the object returned there is intend to be used as a param right away so the following code does not work
[params setValue:[LCSingletonLoggedInUser sharedLoggedInUser].sessionToken forParam:#"token"];
[params setData:obj.HTTPBody forParam:#"data"];
I've also tried various combinations of setObject and setData and obj.HTTPBody as well as just obj right away.
Actually appending obj to params in any way will always result in it adding escape characters which the server can't handle. Setting params = obj will give the correct values to the server but won't include the token.
How about adding it to queryParams?
NSString *resourcePath = [#"/products" stringByAppendingQueryParameters:_queryParams];
Where queryParams is a NSMutableDictionary where you add your token.

how to do json parsing in iphone

i am using json parsing in my application
and this is my json data is as follow :
{"response" :
{"success":false,"error":
{"code":7,"description":"You are not logged in"}}}
and i want description means "You are not logged in" i my string
so how can i do that
please help me.....
Check the blog post with sample code and step by step parsing of JSON.
http://www.xprogress.com/post-44-how-to-parse-json-files-on-iphone-in-objective-c-into-nsarray-and-nsdictionary/
http://pessoal.org/blog/2008/12/12/iphone-sdk-parsing-json-data/
http://mobileorchard.com/tutorial-json-over-http-on-the-iphone/
We're using CJSONDeserializer (TouchJSON library) in the iPhone app being developed at work.
Just use the following method:
NSDictionary * dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:data error:&error];
where data is of type NSData *. It will convert the JSON string into a dictionary so you could get the value of description as follows:
[[[dictionary objectForKey:#"response"] objectForKey:#"error"] objectForKey:#"description"];

How to Separate HTML Response

I am using FConnect in my app. After the user logs in to his/her Facebook account I am getting his/her information as:
{
"id":"###########",
"name":"Vishnu Gupta",
"first_name":"Vishnu",
"last_name":"Gupta",
"link":"facebook a/c link id=#######",
"education":[{"school":{"id":"######","name":"st.joseph"},"type":"High School"}],
"gender":"male","email":"##########",
"timezone":5.5,
"locale":"en_US",
"verified":true,
"updated_time":"2010-11-27T10:10:25+0000"
}
I want to store user's information in database. To do that, I have to separate the user's information that is id, name, and email.
I tried using componentsSeparatedByString method but I do not understand how to retrieve the information in an array.
Can anyone help me????
The response you are getting is in JSON Format which basically have response in Array & Dictionary.
You will be getting this data in Dictionary..so you can access it by using the key value..
NSLog(#"%#",[NSDictionaryObject valueforkey:#"id"]);
NSLog(#"%#",[NSDictionaryObject valueforkey:#"name"]);
NSLog(#"%#",[NSDictionaryObject valueforkey:#"first_name"]);
Hope this will surely work for you.....
ya.....its corrrect.In detail:
Add JSON SDK then write the below code
SBJSON *json = [[SBJSON alloc] init];
NSDictionary *returnObject = [json objectWithString:InfoName];
NSString #id=[NSString stringWithFormat:#"%#",[returnObject objectForKey:#"id"]];
same for name ,email.
then Add these objects to Array and release the json
JSOn is good to use for "key-value' pair parsing.