Here's the response I am trying to parse.
{"table":"PypersonResponsibleCrewMembersWklyView","numberOfRemainingRecords":0,"rowDefinition":{"attrNames":["EmpOraseq","{PEmpNo","PEmpName","Code","Name","EmpNo","EmpTrdCode","EmpTrdName","EmpFirstName","EmpLastName","NhHrsTot","OtHrsTot","DotHrsTot","NhHrs1","OtHrs1","DotHrs1","NhHrs2","OtHrs2","DotHrs2","NhHrs3","OtHrs3","DotHrs3","NhHrs4","OtHrs4","DotHrs4","NhHrs5","OtHrs5","DotHrs5","NhHrs6","OtHrs6","DotHrs6","NhHrs7","OtHrs7","DotHrs7"]},"rows":[{"attrValues":[211448,"1016","Mr. Demo","REL","aman","1004","PRES","President","Gord","Rawlins",0.5,0.5,0,0.5,0.5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},{"attrValues":[211448,"1016","Mr. Demo","REL","aman","ESS001","PROE","Project Engineer","Nadal","Rafael",0.5,0.5,0.5,0.5,0.5,0.5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},{"attrValues":[211448,"1016","Mr. Demo","REL","aman","CL1001","SS01","System Specialist","Ronald","Bromell",0.5,0.5,0.5,0.5,0.5,0.5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}]}
In this response 'attrNames' contains the keys and 'attrValues' contains the values. Is it possible to parse this using GSON?
I am trying to use com.jayway.jsonpath.JsonPath to read key/values from a JSON string:
String contentAsString = mvcResult.getResponse().getContentAsString();
System.out.println(contentAsString);
Object value = JsonPath.read(contentAsString, "$.key");
But I get the error:
Expected to find an object with property ['key'] in path $ but found 'net.minidev.json.JSONArray'. This is not a json object according to the JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.
Printing contentAsString gives:
[{"firstName":"N","key":"mykey"}]
Is contentAsString not valid JSON?
The blob that you posted is not a valid JSON Object, however, it IS a valid JSON Array.
To read this using JsonPath, try using this:
Object value = JsonPath.read(contentAsString, "$[0].key");
This will get the value of the key object, from the 0th element of the initial array.
i have a json object which contains json data with a key. now i want to extract value from that json object like name, address etc and store them to variables.
controller
json_arr = new JSONArray(j_str);
int count = json_arr.length();
json_o.put("user", json_arr);
j_str contains following data
[{"Bollywood":[{"actor":[{"name":"AA","gender":"Male"},{"name":"BB","gender":"Male"}]}]},{"Hollywood":[{"actor":[{"name":"CC","gender":"Male"},{"name":"DD","gender":"Male"}]}]}]
now it is converted to json object -- json_o ,, putting a key --- "user". now how can get a specific data such as 2nd actor name from hollywood. (i.e value DD). after then store that to a string.
Short answer: Use Jackson to map the json string to a java object, and then extract that value as a variable.
Here is a quick guide on doing this with jackson: http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/
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 "]"