{"something":"1","mode":"true","number":"1234"}
Because I'm getting a 406 on expecting JSON.
It's being generated via Jersey, which is told that a method #Produces JSON. It's being received by a Dojo xhrGet which has JSON set as its handleAs.
EDIT - To clarify, I'm not interested in the code where I evaluate or anything like that. The question was very simple - is it valid JSON?
It is, but you've got both the boolean (mode) and numeric (number) elements as strings. Shouldn't it be:
{"something":"1","mode":true,"number":1234}
It is valid JSON if all values of the dictionary are Strings. This is also valid JSON:
{"something": 1, "mode": true, "number": 1234}
Usually, however, a 406 error happens when you ask for a response type (such as html or json) and the server cannot send it in that type. Why do you think the input is invalid?
I use a simple copy/paste tool called JASONLint ( http://www.jsonlint.com/ ) to test my mountains of JSON. You may dig it.
If you want to use the numbers directly, you shouldn't put them in quotes. It is valid JSON, but chances are that what you want to do is:
{"something":1,"mode":"true","number":1234}
You need to add more information if you want better answers.
EDIT: Eh... and yes, the boolean shouldn't be quoted either, unless you want to convert it yourself, for some reason.
yes this is valid JSON
although if you're planning on outputting this as the result of a HTTP request, you'll need to escape all the quotes
$str = "{\"something\":\"1\",\"mode\":\"true\",\"number\":\"1234\"}";
echo $str
Related
I am getting the following json object when I call the URL from Browser which I expect no data in it.
"{\"data\":[], \"SkipToken\":\"\", \"top\":\"\"}"
However, when I tried to call it in javascript it gives me error Parsing Json message
dspservice.callService(URL, "GET", "", function (data) {
var dataList = JSON.parse(data);
)};
This code was working before I have no idea why all of a sudden stopped working and throwing me error.
You say the server is returning the JSON (omitting the enclosing quotes):
{\"data\":[], \"SkipToken\":\"\", \"top\":\"\"}
This is invalid JSON. The quote marks in JSON surrounding strings and property names should not be preceded by a backslash. The backslash in JSON is strictly for inserting double quote marks inside a string. (It can also be used to escape other characters inside strings, but that is not relevant here.)
Correct JSON would be:
{"data":[], "SkipToken":"", "top":""}
If your server returned this, it would parse correctly.
The confusion here, and the reports by other posters that it seems like your string should work, lies in the fact that in a simple-minded test, where I type this string into the console:
var x = "{\"data\":[], \"SkipToken\":\"\", \"top\":\"\"}";
the JavaScript string literal escaping mechanism, which is entirely distinct from the use of escapes in JSON, results in a string with the value
{"data":[], "SkipToken":"", "top":""}
which of course JSON.parse can handle just fine. But Javascript string escaping applies to string literals in source code, not to things coming down from the server.
To fix the server's incorrectly-escaped JSON, you have two possibilities. One is to tell the server guys they don't need to (and must not) put backslashes before quote marks (except for quote marks inside strings). Then everything will work.
The other approach is to undo the escaping yourself before handing it off to JSON.parse. A first cut at this would be a simple regexp such as
data.replace(/\\"/g, '"')
as in
var dataList = JSON.parse(data.replace(/\\"/g, '"')
It might need additional tweaking depending on how the server guys are escaping quotes inside strings; are they sending \"\\"\", or possibly \"\\\"\"?
I cannot explain why this code that was working suddenly stopped working. My best guess is a change on the server side that started escaping the double quotes.
Since there is nothing wrong with the JSON string you gave us, the only other explanation is that the data being passed to your function is something other than what you listed.
To test this hypothesis, run the following code:
dspservice.callService(URL, "GET", "", handler(data));
function handler(data) {
var goodData = "{\"data\":[], \"SkipToken\":\"\", \"top\":\"\"}";
alert(goodData); // display the correct JSON string
var goodDataList = JSON.parse(goodData); // parse good string (should work)
alert(data); // display string in question
var dataList = JSON.parse(data); // try to parse it (should fail)
}
If the goodData JSON string can be parsed with no issues, and data appears to be incorrectly-formatted, then you have the answer to your question.
Place a breakpoint on the first line of the handler function, where goodData is defined. Then step through the code. From what you told me in your comments, it is still crashing during a JSON parse, but I'm willing to wager that it is failing on the second parse and not the first.
Did you mean that your JSON is like this?
"{\"data\":[], \"SkipToken\":\"\", \"top\":\"\"}"
Then data in your callback would be like this:
'"{\"data\":[], \"SkipToken\":\"\", \"top\":\"\"}"'
Because data is the fetched text content string.
You don't have to add extra quotes in your JSON:
{"data":[], "SkipToken":"", "top":""}
I am trying to parse this JSON:
var json = '{"material":"Gummislang 3\/4\" 30 m (utanp\u00e5liggande sk\u00e5p)"}'
I run JSON.parse(json) but i get the error SyntaxError: Unexpected number when doing so. I have tried this in Google Chrome. I don't know what the problem is since I can take the JSON string and put it in any JSON validator and it claims that the JSON is valid. Shouldn't the browser be able to parse it?
You are inserting a JSON object representation into a JavaScript string without properly escaping the representation.
To avoid having to do this, remove the quotes you are adding around the representation, and skip the JSON.parse(json) – the default output from PHP's json_encode() is valid JavaScript when used in this context.
For security, you should specify the JSON_HEX_TAG option if possible. This will prevent cross-site scripting in cases where the JSON might end up inside a document parsed as XML. (And for XML documents, the JSON should be inside a CDATA section as well.)
You're validating the string literal, which is a valid JSON string containing invalid JSON. You need to validate the value of the string, which is not valid JSON.
If you paste the string value into a JSON validator, you'll see that the error comes from this part:
"material": "Gummislang 3/4"30m
The " needs to be escaped.
I am having a problem with special character in javascript.
I have a form with a input text that has the following string:
10/10/2010
after a form.serialize(); I get this string as
10%2F10%2F2010
The '/' character is converted to its ASCII code %2F.
I would be able to convert that using String.fromCharCode(ascii_code) but I have many inputs in my form so these string is somenthing like:
var=14&var=10%2F10%2F2010&var=10%2F10%2F2010&var=10%2F10%2F2010
Just an example to state that I would have to go through this string ("manually") and find those value and convert it.
Is there any easy way to perform that conversion?
Strange thing because I did not have that problem before, I am not sure why this is happening now.
I happens that way because that's how it's meant to be:
The .serialize() method creates a text string in standard URL-encoded
notation. It operates on a jQuery object representing a set of form
elements.
As far as I know, there's no native jQuery function to unserialize but your post suggests you already got that and are only stuck in the URL-encoded strings:
decodeURIComponent(encodedURI)Decodes a Uniform Resource Identifier (URI) component previously created by encodeURIComponent or
by a similar routine.
this question might have been asked already. But i really have no idea what to search for.
If I have a string like
{{aa:bb,aaa:bbb,cc:ee{{aa:cd,cdc:dd,{{ss:ee}},kk:ee}},se:ff}}
I need to get output in probably in array
ar[0] = aa:bb, ar[1]=aaa:bbb, ar[3] = {{...}}
I tried using variable.split("}}")
which is breaking the string and not getting the actual data.
Is there any recursive function to do this?
I am not able to search because I have no clear idea of what objects,strings.
If you used an existing format for structuring your string, such as JSON:
["aa:bb","aaa:bbb","cc:ee",["aa:cd","cdc:dd",["ss:ee"],"kk:ee"],"se:ff"]
Then you could just run it through JSON.parse(). - It'd be far easier than trying to decode the meaning of that string without being told what it means.
I think what you're looking for is how to parse a JSON string into an object. I'm not certain, but at least it looks like that based on the format of your string. Can you confirm if the source is providing JSON output?
If yes:
Read this other SO question.
I am new to using JSON. On subscribing to a webservice I receive a json response as given below.
1024760833990-36891Customercustomer realtime20110914 10:48:10NNNYYYYN{"hostName":"uat91w82m7","data":{"view":{"columnValues":[{"DisplaySymbol":"MSFT Jan 19 '13 $35 Call","Symbol":"MSFT--130119C00035000","Quantity":1.0,"Price":0.71,"ChangeValue":0.01,"ChangePercentage":1.41,"DaysGainValue":1.0,"PriceAdjusted":false}],"columnHeaderCodes":[1,2,3,4,11,5],"viewName":null,"quoteType":0,"accountNumber":"39903689","asOfDate":1316022555984,"totalMarketValue":"71.0","todaysGainValue":"1.0","annualGainValue":"0.0","pagination":{"nextPositionMarker":"","pageNumber":1,"posPerPage":500,"posDetailPerPage":50,"totalNumberofPositions":1,"markerLength":0},"viewType":3,"portfolioId":null,"customView":false,"displayNetWorth":1,"groupOptions":"G0","viewID":null,"widgetType":null,"columnHeaders":null,"totalPositionCount":0,"easternDaylight":true,"widget":false}},"smUser":"102476083","success":true,"sysdate":1316022555992,"message_info":null,"message_type":null}
I am trying to display certain parameters on my page. So how to I parse it.
ANSWER : just remove 1024760833990-36891Customercustomer realtime20110914 10:48:10NNNYYYYN through PHP or any other server side script and them parse it to jQuery, i'm damn sure it will do the job.
First paste your JSON into JSONLint.com to make sure it's valid JSON. What you provided in your question is not valid.
Secondly you can parse it with JQuery using parseJSON or with old skool JS using JSON.parse.
As long as you have a correctly formatted JSON string, all you have to do is use JSON.parse(string).
var JSON_string='{"name":"Jason","age":22}';
var JSON_object=JSON.parse(JSON_string);
console.log(JSON_object.name+' is '+JSON_object.age);