I am receiving data from the server using Volley, but the received data is coming within double quotes.
"{"Meta Data":{"1. Information":"Daily Prices (open, high, low, close) and Volumes","2. Symbol":"INSI","3. Last Refreshed":"2017-11-24","4. Output Size":"Full size","5. Time Zone":"US/Eastern"},"Time Series (Daily)":{"2017-11-24"....}"
I need to parse this information in order to extract the data but I am unable to convert it into JSONObject. I am clueless.
On using jsonArray = new JSONArray(response);
The debugger says jsonArray is NULL
TIA.
#jyotirmaya ojha, the JSON string that you shared have JSON Object not JSON Array, so the debugger will always say jsonArr is NULL because JSON Object cannot be casted in JSON Array
Please try this instead
JSONObject jsonObj = new JSONObject(response);
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 convert a string into a JsonArray. So far I have tried to do the following:
Gson().toJson(string)
Gson().toJsonTree(string)
both throw an exception saying that the argument is not a JsonArray.
Here is the string, as you can see it is a JsonArray:
"[{\"match\":{\"id\":92757102,\"tournament_id\":3666234,\"state\":\"open\",\"player1_id\":58602461,\"player2_id\":58602459,\"player1_prereq_match_id\":null,\"player2_prereq_match_id\":null,\"player1_is_prereq_match_loser\":false,\"player2_is_prereq_match_loser\":false,\"winner_id\":null,\"loser_id\":null,\"started_at\":\"2017-07-17T19:10:07.588-04:00\",\"created_at\":\"2017-07-17T19:10:07.476-04:00\",\"updated_at\":\"2017-07-17T19:10:07.588-04:00\",\"identifier\":\"A\",\"has_attachment\":false,\"round\":1,\"player1_votes\":null,\"player2_votes\":null,\"group_id\":null,\"attachment_count\":null,\"scheduled_time\":null,\"location\":null,\"underway_at\":null,\"optional\":false,\"rushb_id\":null,\"completed_at\":null,\"suggested_play_order\":1,\"prerequisite_match_ids_csv\":\"\",\"scores_csv\":\"\"}}]"
JsonArray data = (JsonArray) JsonParser.parseString(str);
Gson().fromJson(string, JsonArray::class.java)
toJson() renders a json object as a string (of json).
You want the fromJson() method, which converts a string to a json object.
Try:
new Gson().fromJson(string, JsonArray.class)
This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 7 years ago.
Using a GET request to get an artist (search) from the echonest API I get the following JSON back:
{
"response": {
"status": {
"version": "4.2",
"code": 0,
"message": "Success"
},
"artists": [
{
"id": "ARR3ONV1187B9A2F49",
"name": "Muse"
}
]
}
}
I want to convert the above JSON string to a JSON object like this:
jso = new JSONObject(JSONstring);
Then I want to save both the id and name into strings, first I want to save the array of artists in a JSON array like this:
jsa = jso.getJSONArray("artists");
This is the moment where I get the JSON error no value for artists.
I can't figure out what is going wrong here, can anyone help me in the right direction? Thanks.
The json array artists is inside the json object response
So you have to get the Json object with key response first, then get
json array artists from it
jsa = jso.getJSONObject("response");
jsa.getJSONArray("artists");
Artists array is response JSON object so first you to get response data and then after you get artists JSON array like below
JSONObject response = jso.getJSONObject("response");
jsa = response.getJSONArray("artists");
Hope it will help you !!
The reason you are not getting value is because you are not following nesting.
You have to receive object like its nested. First you have to goto MainObject and then data is stored in "response" object. After that you can receive array from "response" object.
You must follow nesting of JSON like it designed.
Try Doing it this way..
JSONObject mainObj = new JSONObject(JSONstring);
// Try to get Response Object from main JSON object then try to get array from it.
JSONObject responseObj = mainObj.getJSONObject("response");
// getting array from response object.
JSONArray artistArr = responseObj.getJSONArray("artists");
Or you can do it in one line..
JSONArray artistArr = mainObj.getJSONObject("response").getJSONArray("artists");
You have it nested within the response object so you are currently trying to access "artists" like it is the 'parent' object but this is in fact a child of the "response" object. You will need to first retrieve the json object to "response" and then get the json array "artists".
"artists" array is inside the "response" object, you can get the "artists" array using following code:
jsonArray = jso.getJSONObject("response").getJSONArray("artists");
I need to parse json which I get from YQL but I am having trouble as I am not getting the results I need. I am using simple json (https://code.google.com/p/json-simple/wiki/DecodingExamples) and trying to follow the documentation. The problem is the example they show are very limited (I am very new to json). I want to extract everything in the array (Sy, Date, O, H, L, C, and V ). In the documentation they show how to extarct elements from an array if the json object is just as an array, but I have an array + some extra stuff on top:
{"query"
{"count":200,"created":"2014-06-17T00:46:43Z","lang":"en-GB","results"
This is the full json object, how would I extract just the array?
{"query"
{"count":200,"created":"2014-06-17T00:46:43Z","lang":"en-GB","results"
{"array":[{"Sy":"Y","Date":"2010-03-10","O":"16.51","H":"16.94","L":"16.51","C":"16.79","V":"33088600"},
{"Sy":"Y","Date":"2010-03-09","O":"16.41","H":"16.72","L":"16.40","C":"16.53","V":"20755200"},
{"Sy":"Y","Date":"2010-03-08","O":"16.32","H":"16.61","L":"16.30","C":"16.52","V":"30554000"}
]}}}
i use https://code.google.com/p/org-json-java/downloads/list
this is simple
try{
String json = "JSON source";
JSONObject j = new JSONObject(json);
JSONArray arr = j.getJSONObject("query").getJSONObject("results").getJSONArray("array");
for(int i=0; i<arr.length(); i++){
JSONObject obj = arr.getJSONObject(i);
String sy = obj.getString("Sy");
String date = obj.getString("Date");
String o = obj.getString("O");
String h = obj.getString("H");
String l = obj.getString("L");
String c = obj.getString("C");
String v = obj.getString("V");
}
}
catch(JSONException e){
}
You have to extract the array you need piece by piece.
JSONParser parser=new JSONParser();
String s="{YOUR_JSON_STRING}";
JSONArray array=parser.parse(s).get("query") //"query"
.get("result") // "query->result"
.get("array"); // THE array you need
Note that you might need to use try...catch... block to deal with exceptions.
Since you are using java, I highly recommend gson, which is written by google. It can convert json to object directly, which means you don't need to get the array deep inside the json step by step. https://code.google.com/p/google-gson/
Generally speaking, you can use gson to parse json piece by piece with jsonparser or, convert the whole json to a object with gson.
the string I have into "jsonString" is the content of this link: http://85.18.173.82/cineca/wp5/json/events.json
Now I want the value "Day" of the second "Event".
JSONObject o = new JSONObject(jsonString);
String day = o.getString("XXXXXXXXXX");
System.out.println(day);
What does I have to put as argument of o.getString?
Many thanks
JSONObject obj = new JSONObject(json);
JSONArray array = obj.getJSONArray("Events");
for(int i = 0 ; i < array.length() ; i++){
System.out.println(array.getJSONObject(i).getJSONObject("Event").getString("Day"));
}
In this way, you can access, thanks.
The way you're constructing your JSONObject is wrong. By using this constructor you're not reading the json from that URL, you're actually using that string as a json representation (which it is not).
If you want to first read the json from your URL you'll have to do an HTTP GET request and then construct a JSONObject out of the response.
For more info, take a look at JSONObject docs