Parse JSON string to detect error response - delphi

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

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

Get request response is in weird format

I send a get request to a local (separate from app) jetty web server
RestClient.get("ip/command/core/get-version", {})
Then I do a JSON.parse() on the response.
As a result I get
{"revision"=>"r2407", "full_version"=>"2.5 [r2407]", "full_name"=>" [r2407]", "version"=>"2.5"}
What's wrong? How do I turn it into a hash, so I can extract the full_version property?
String returned by service is html encoded. Try decoding it first:
JSON.parse(CGI.unescape_html(response_body))
Your JSON response looks to be encoded into HTML entities.
If you are using Ruby, try decoding the response using CGI.unescape_html prior to running JSON.parse. Running the result of that method through JSON.parse should give you your hash.

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/.

Gzip decompress JSON POST body in Rails/Passenger/Nginx

We have a function in our Rails code that accepts a JSON POST body:
contacts = ActiveSupport::JSON.decode(request.raw_post.gsub("+", ""))
(I'm aware that I can get this from params["_json"] as well, but we have extremely large (MBs) POST bodies that do not get put into params["_json"] for some reason (and + throws errors too).
Since the JSON is usually sent from a mobile client, it's important to us to optimize the upload size. We want to switch to having the POST body gzipped.
However, no matter what we do, we get the same error with no line number:
MultiJson::DecodeError (743: unexpected token at ''):
We have tried:
gzipped_contacts = Zlib::GzipReader.new(StringIO.new(request.raw_post)).read
contacts = ActiveSupport::JSON.decode(gzipped_contacts.gsub("+", ""))
This:
gzipped_contacts = ActiveSupport::Gzip.decompress(request.raw_post)
contacts = ActiveSupport::JSON.decode(gzipped_contacts.gsub("+", ""))
And the solution found here: Rails: how to unzip a compressed xml request body?
I'm pretty sure this is not occurring at the controller level because I can't log anything there, so it needs to be done in the middleware or at the server (but I can't find anything for Nginx that lets us deflate). Please assist!
Ok, turns out the iPhone client was sending the wrong headers. So the solution for anyone encountering this is to see the advice here:
Rails: how to unzip a compressed xml request body?
And verify that you are sending Content-Type: gzip/json.

Adobe Flex 3 : Fault Event doesnt return XML Feed sent from Server

I am working on a flex application which communicates with a Rails backened.
When i request for some data, It sends back xml feed.
In some cases, if given parameters are not valid, then rails return an error feed with status code = 422 as following
email is wrong
But I dont get this feed in FaultEvent of Flex, How could i read error feed?
Thanks
Are you getting the result in ResultEvent in such cases? I am not sure for what all HTTP error codes FaultEvent will get invoke(I know only it goes for 404 and 500). May be its still going to ResultEvent as a valid result!
You can use HTTPService instead of URLLoader.
Flex HTTP results will not include the actual underlying HTTP response codes. It just doesn't work. (TM)

Resources