JSONValue doesn't parse the JSON response - ios

when i try to parse the response returned from a web service i got this stack:
JSONValue failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Unrecognised leading character\" UserInfo=0x6e76c30 {NSLocalizedDescription=Unrecognised leading character}"
)
My code is like this:
if(request.responseStatusCode==200)
{
NSLog(#"so far, this block of code works");//this log is displayed
//parse the response
NSArray *array=[[request responseString]JSONValue];
//processing the array and do whatever
In this thread they talking about an old version of the JSON library and how to update it, please help me figure out what should i do.
EDIT:
when trying to parse the response, i wanted to see why the server return, so an NSLog like so:
if(request.responseStatusCode==200)
{
NSLog(#"the request is %#", request.responseString);
//parse the response
NSArray *array=[[request responseString]JSONValue];
//processing the array and do whatever
give me this stack:
the request is <br />
<b>Warning</b>: implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>/homepages/25/xxxx/htdocs/my app/webservices/index.php</b> on line <b>39</b><br />
<br />
<b>Fatal error</b>: Call to undefined function sendResponse() in <b>/homepages/25/xxxxx/htdocs/my app/webservices/index.php</b> on line <b>57</b><br />
2012-01-20 15:10:45.491 my-iPhone-application[882:11603] -JSONValue failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Unrecognised leading character\" UserInfo=0x6ebd450 {NSLocalizedDescription=Unrecognised leading character}"
)
EDIT 2:
My application sent an array that is supposed to be received and processed in the web services. So one example of how the array received looks like is:
(
Mairie,
\"Pr\\U00e9fectures et sous-pr\\U00e9fectures\",
\"Auto-\\U00e9cole\"
)
The strange ant-slashes and 00 are the é caracter. Am i wrong to use them in my data? Is this which is causing my problem?

The error pretty much says it all, the parser does not consider the JSON string is it reading to be valid JSON. "Unrecognised leading character" means that it found something other than a { or [ character as the first character in the string returned from [request responseString]
If you post the raw JSON we might be able to help you point out exactly what is wrong with the response.
HTH

If request.responseString returns <br /> then it looks like your server is returning HTML and HTML isn't JSON ;)
Your use of the JSON library is correct, it's either your request or (more likely given the response) your server thats broken.
On the plus side, you have accidentally done some error testing on your app :)

Related

iOS SDK SOAP parsing error : Data at the root level is invalid. Line 1, position 1

I am developing an iPhone app. I have to get data from .NET server. The response is in the following form.
<string xmlns="http://tempuri.org/">
{"success":1,"message":"Data Save Successfully.","returnvalue":"106"}
</string>
I am using NSXMLParser to parse to parse it. I want to get the json string . But the parser is giving error
Server was unable to process request. ---> Data at the root level is invalid. Line 1, position 1.
How can i fix the error.
Thanks in advance
The String in the respons is a JSON, So i think the NSXMLParser don't work with a JSON input. The parsing error is because the syntax of XML is different then from JSON.
This link will help you to read the String JSON response you get.
how to parse json with swift2

Are all elements in a POST requests body mandatory?

If a POST request requires that i send four data elements in the body, like:
{
"name":abc,
"surname":xyz,
"contact_no":1234,
"address":random_value
}
What will happen if I miss out some of the elements, for example if I call the web service with elements like,
{
"name":abc,
"surname":xyz,
}
What error is the server likely to throw ? I am having this issue because I am making a API call and it's giving me a HTTP error 500, so I guess it's not an error on my part, but this is one doubt i wanted to clear. Thanks in advance.
Its depends on the service structure (if all fields are mandatory or not),
and HTTP error (500) returns to server error or (server or link) not found .

restkit google maps

I'm using RestKit v0.20.0-pre6 for iOS. My input is JSON from a service running on Google App Engine. This is what is being returned from the service:
{
action:"NOP",
payload:null,
timeStamp:new Date(1359427714679),
type:null
}
This is the error I'm getting displayed from RestKit in the output window:
E restkit.network:RKResponseMapperOperation.m:240
Failed to parse response data: Loaded an unprocessable response (200) with content type 'application/json'
It is choking when NSJSONSerialization is called to parse the data. I'm sure it is the line that contains new Date(1359427714679), but I am unsure on how to parse this line. The RestKit documentation mentions writing your own formatter, but I'm unclear on how to do this.
Any insight on how to solve this issue would be much appreciated.
You've got a couple of problems with your JSON here. Firstly, your keys need to be in inverted commas "". Secondly, your web service needs to provide the timestamp as a string.
Try ISO8601 format, e.g.
{
"action" :"NOP",
"payload" : null,
"timeStamp" : "1901-12-13T20:45:52+00:00",
"type": null
}
You can check that your JSON is valid by pasting it into an online validator such as http://www.jslint.com/.

NSJSONSerialization gives me error "Duplicate key"

I request a JSON response from a home controlling device:
NSData* responseData = [NSData dataWithContentsOfURL:url];
The JSON data in responseData is complete and valid. However, if I try
NSDictionary* dict = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSLog("Error: %#", error);
it gives me the following error:
Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Duplicate key for object around character 0.) UserInfo=0x6a8b8e0 {NSDebugDescription=Duplicate key for object around character 0.}
I searched the web but couldn't find anything helpful so far.
Edit: The JSON response it quite long, so instead of posting it here, please find it here.
It looks like the JSON is fine by pasting it into http://jsonformat.com/ - maybe you found an Apple bug? Another way to try and validate would be to use a third-party iOS library and see if it gives you the same error? Say, TouchJSON?
I get the same error, only at random when the app runs. When the NSJSONSerialization fails I've checked the json object it chocks on using a JSON Viewer. The JSON is always OK. Bugs in NSJSONSerialization when it's called several times?
I got the same error. My JSON data passed JSON validator at http://jsonformatter.curiousconcept.com/ . However, I found two duplicated keys in my JSON (at the end of dictionary). After I removed those keys, NSJSONSerialization works smoothly.

Parse JSON string to detect error response

i'm working with a server which response using the JSON format.
when the request contain valid data they respond with a string like this
{"data":{"results":[{"Branch":"ACCT590006"}]}}
but if the parameters of the request are incorrect the response goes like this
{"error":{"errors":[{"domain":"global","reason":"invalid","message":"Invalid
Params"}],"code":98865,"message":"Invalid
param value"}}
So the questions are how i can determine when the response of the server contains a error string using the TJSONObject object and additionally parse the JSON string to show the messages and error codes like this.
Failed reason : invalid
Message : Invalid params
Code: 98865
message : invalid param value.
I've worked a little with JSON, an every time I've parsed from code(delphi 7). But i've searched a little bit, and here you may find the answer of your question:
http://edn.embarcadero.com/print/40882
and with a little adaption this should work.
Best regards,
Radu

Resources