I am trying to convert a JSON object to JavaScript object. JSON object i received from webservice
contain java object
{
gateway:com.admin.mypackage.addreesoftheobject;
}
JSON.parse in JavaScript, failed to parse this field, throwing exception, invalid character 'c'
is there any other way of parsing my JavaScript object.
Thanks and Regards
Biswarup
The problem is it's incorrect JSON. You can use validators for checking:
jsonlint
jsonformatter
if you change your JSON to :
{
"gateway":"com.admin.mypackage.addreesoftheobject;"
}
it should be parsed correctly.
Related
I have a piece of json, but when I go to deserialize it, it won't map correctly to an object in C#.
Ex:
"location": {
"raw": "{\"address1\":\"1234 Fake Street\",\"address2\":null,\"city\":\"Madison\",\"state\":\"WI\",\"zip\":\"12345\",\"country\":\"US\"}"
},
I'm using restSharp to make the api call and Newtonsoft.Json
var test = JsonConvert.DeserializeObject(content);
I've tried the following and failed:
Parsing the json
Replacing the backslashes
Serializing, then deserializing again
JavascriptSerializer
Is there anyway to deserialize a string into an object with the backslashes there?
exercise question screenshot
Not sure what's missing. The other discussion solutions seem too complicated compared to my homework question:
Q:
-The variable str_json has been assigned a string of a JSON object
-Call the parse method, pass it str_json and assign the return value to variable jsonobj
-Assign the property the_city to the variable v_the_city
-Assign the property stateval to the variable v_stateval
var str_json = {'v_the_city':'the_city','v_stateval':'stateval'};
var jsonobj = JSON.parse(str_json);
SyntaxError:
the JSON dataJSON.parse: unexpected character at line 1 column 2 of
SyntaxError: unexpected token: identifier
str_json should be a JSON string, not an object.
The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned. Read more here, link.
Syntax:
JSON.parse(text[, reviver])
Parameters:
text - The string to parse as JSON. See the JSON object for a description of JSON syntax.
reviver - [Optional] If a function, this prescribes how the value originally produced by parsing is transformed, before being returned.
I need to pass an array of string/Int (doesnt matter) as JSON parameter in a HTTP body for a POST request.
{
"par1" : value,
"par2" : "value2",
"par3" : ['123:456', '123:234' ...]*
}
*My problem is for filling my JSON object with param3 values. In the swift code I have them as an array of Strings.
There are many example of converting a JSON object back to an array of strings/Int, but I can't find it the other way around.
As mentioned in the comments, the way to go is to use NSJSONSerialization to generate object encoded in JSON data.
I have the following code to fetch data from Json object
System.Console.WriteLine("Error Create Order: {0} {1}", postOrderResult.StatusCode, postOrderResult.ReasonPhrase);
var orderResults = postOrderResult.Content.ReadAsAsync<List<OrderResult>>().Result;
But its returning error
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ABCS.WebApi.Models.OrderResult]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'Message', line 1, position 11.
Please help
Seems like you are passing a simple JSON object while your data type is expecting for an array of JSON objects.
Try encasing your current JSON data between "[" and "]"
How do I parse json like this:
[{"name":"joe","address","Main Street"}]
with the from() method in restassured:
assertEquals(true, from(myjson).getString("name"));
I get this error:
[Fatal Error] :1:1: Content is not allowed in prolog.
My guess would be you're using XmlPath.from() instead of JsonPath.from(), so it's trying to parse your JSON as XML.