I am having problem while consuming/deserializing WCF .NET JSON response in Java. JSON response is in the following format.
{"d":"[
{\"ID\":123,\"Company\":\"Microsoft\",\"Country\":\"USA\",
\"website\":\"http:\/\/www.microsoft.com\",
\"FirstName\":\"john\",\"Email\":\"abc#gmail.com\"},
{\"ID\":124,\"Company\":\"Google\",\"Country\":\"USA\",
\"website\":\"http:\/\/www.google.com\",
\"FirstName\":\"john\",\"Email\":\"abc#gmail.com\"},
{\"ID\":125,\"Company\":\"Apple\",\"Country\":\"USA\",
\"website\":\"http:\/\/www.abc.com\",
\"FirstName\":\"john\",\"Email\":\"abc#gmail.com\"}
]"}
While on the Java code side I am having problem to deserialize this json response to get out my objects and their corresponding properties.
This is the java code currently I am using to deserialize json response.
String companyTitle = "";
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(jsonResponseString);
if (element.isJsonObject()) {
JsonArray companies = element.getAsJsonArray();
JsonObject company = companies.get(0).getAsJsonObject();
companyTitle = company .get("Company").getAsString();
}
Is there any problem in the JSON response format or its right? any kind of help is appreciated, thanks in advance.
I'm not sure why you're getting that response - it't not valid json. There are two things wrong with it
The outer square brackets should not be wrapped in quotes.
The quote escape characters need to be removed (not sure if this is just you putting them in?)
Without you posting the actual error you get (hint: even though stack overflow is powerful, we have not yet developed the ability to read minds) it's very difficult to know what the actual problem is.
Tom is right. Valid JSON should look like this:
{"d":[
{"ID":123,"Company":"Microsoft","Country":"USA",
"website":"http://www.microsoft.com",
"FirstName":"john","Email":"abc#gmail.com"},
{"ID":124,"Company":"Google","Country":"USA",
"website":"http://www.google.com",
"FirstName":"john","Email":"abc#gmail.com"},
{"ID":125,"Company":"Apple","Country":"USA",
"website":"http://www.aabc.com",
"FirstName":"john","Email":"abc#gmail.com"}
]}
And your code like this:
String companyTitle = "";
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(jsonResponseString);
JsonObject object = element.getAsJsonObject();
if (object.isJsonObject()) {
JsonArray companies = object.getAsJsonArray("d");
JsonObject company = companies.get(0).getAsJsonObject();
companyTitle = company .get("Company").getAsString();
}
Related
I'm new to using JSONs and I'm having a bit of difficulty. I am using the Kitsu API and parsing the JSON I get when I login. When I parse my json the image below pops up, but inside the 1 object array I want to get the large url in the avatar object inside of the attributes object and I don't know how.The beginning of the JSON, The middle of the JSON, The end. Lastly, I want to know how to edit the slug part of the JSON, if you don't know that's cool the main thing is getting the avatar url.
Picture of my error
Please, try this...
JSONObject myObject = new JSONObject(jsonStr);
JSONArray myArray = myObject.getJSONArray("data");
JSONObject obj = myArray.getJSONObject(0);
String id = obj.getString("id");
String type = obj.getString("type");
//change the lines bellow
JSONObject attributes = obj.getJSONObject("attributes");
JSONObject avatar = attributes.getJSONObject("avatar");
String large = avatar.getString("large");
I am trying to parse json and running into a small problem.
my json string looks like this:
String json =
[
"{caption=blah, url=/storage/emulated/0/DCIM/Camera/20140331_164648.jpg}",
"{caption=adsf, url=/storage/emulated/0/DCIM/Camera/20140330_103412.jpg}"
]
and my code so far looks like this:
try {
JSONArray jsonObj = new JSONArray(json);
for (int i = 0; i < jsonObj.length(); i++) {
JSONObject c = jsonObj.getJSONObject(i);
String img = c.getString("url");
String cap = c.getString("caption");
But its throwing an exception type java.lang.String cannot be converted to JSONObject
What am I doing wrong?
EDIT
If its helpful to anyone, I ended up using GSON to get my json in the correct expected format like this:
Gson gson = new Gson();
String json = gson.toJson(mylist);
Your JSON array contains elements like
"{caption=blah, url=/storage/emulated/0/DCIM/Camera/20140331_164648.jpg}"
which is a String not a JSON object. You can't therefore try to retrieve it as a JSONObject.
Seems like you're getting JSON that isn't in the format you expected. Even if you got rid of the "" around it, it still wouldn't be valid JSON, so I don't understand its purpose enough to help you.
I am using Java to parse a JSON response from a server. My end goal is to have the data from results in an Array. Currently I am using this to try and get the results:
JSONArray jArray = myResponse.getJSONArray("results");
This code fails because it is looking for an array of objects, rather than an array of strings:
org.json.JSONException: Value blah at 0 of type java.lang.String cannot be converted to JSONObject
This is my server's JSON Response:
{
status: "OK",
results: [
"blah",
"bleh",
"blah"
]
}
Is there a simple way to get the "results" value into an array? Or should I just write my own parser.
Thanks
---------- UPDATE ----------
Looks like my problem was actually occuring somewhere else, and not where the JSON attribute "results" was being converted into a JSONArray.
Sorry and thanks for the answers, they helped me realize I was looking in the wrong spot.
This should be it. So you're probably trying to get JSONObject instead of String inside the results aarray.
JSONObject responseObject = new JSONObject(responseString);
JSONArray resultsArray = responseObject.getJSONArray("results");
for (int i=0; i<resultsArray.length(); i++)
String resultString = resultsArray.getString(i);
As you will probably have more properties, than only the String[] result, I recommend to define a DTO like this:
public class Dto {
//of course you should have private fields and public setters/getters, but this is only a sample
public String status;
public List<String> results;//this can be also an array
}
And then in your code:
ObjectMapper mapper = new ObjectMapper();
Dto dto = mapper.readValue(inputDtoJson, Dto.class);//now in dto you have all the properties you need
This is my String gathered from a JSON file. It is as expected:
{
"catalog":{
"book":{
"id":"bk101",
"author":"Gambardella, Matthew",
"title":"XML Developer's Guide",
"genre":"Computer",
"price":"44.95",
"publish_date":"2000-10-01",
"description":"An in-depth look at creating applications with XML."
}
}
}
Using this String I created a JSONObject..
JSONObject jsonBook = new JSONObject(sb.toString());
Then I am simply trying to extract some of the parameters seen in the string such as id and author.
"id": "bk101"` `"author": "Gambardella, Matthew"
This is my approach..
book.setAuthor(jsonBook.getString(Book.AUTHOR));
book.setId(jsonBook.getString(Book.ID));
Yet I keep getting errors saying there is no value for id/author whichever one is first. Any ideas?
Cheers
That's because id and author are both inside book, not inside the json root
JSONObject jsonBook = new JSONObject(sb.toString());
JSONObject catalogue = jsonBook.getJSONObject("catalog");
JSONObject jbook = catalogue.getJSONObject("book");
book.setAuthor(jbook.getString(Book.AUTHOR));
book.setId(jbook.getString(Book.ID));
I know it is a simple question .But I can't find a answer.
How to parse this Jquery ajax.
My Jquery ajax success response will be,
{"status" : "ready"};
Help me to do this.
My Jquery ajax success response will be,
From the above line , I assume you are looking to parse the JSON response in the client side. In javascript you can parse it as :
var json = '{"status" : "ready"};',
var obj = JSON.parse(json);
In Java , using GSON :
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(yourJsonString);
JsonObject jsonObject = element.getAsJsonObject();