I'm trying to read JSON but I got below error.
2019-08-27 13:12:43.352 27379-27379/com.test W/System.err: org.json.JSONException: Value transcript of type java.lang.String cannot be converted to JSONObject
JSON
{"transcript":{"text":[{"content":"Flood!","dur":"1.76","start":"2.28"},{"content":"Listen up, there’s a flood coming!","dur":"2.24","start":"4.52"},{"content":"Oy lads, flood!","dur":"1.92","start":"7.88"},{"content":"There’s a flood –","dur":"1.24","start":"10.72"},{"content":"Maurice, they’re not listening.","dur":"1.84","start":"13.96"},{"content":"We’ve got to give them\na good old-fashioned fish slappin’.","dur":"2.8","start":"15.96"},{"content":"Fish Slap!","dur":"1.04","start":"20.52"},{"content":"Fish Slap!","dur":"1","start":"22.32"},{"content":"Fish Slap!","dur":"1.2","start":"24.12"},{"content":"Double Fish Slap!","dur":"1.24","start":"25.84"},{"content":"See, guys? Flood’s here.","dur":"1.56","start":"27.84"},{"content":"Tub Rider!","dur":"2.48","start":"31.56"},{"content":"SEASON 1\nTHE FLOOD","dur":"1","start":"35.32"},{"content":"Because I’m in a tub!","dur":"1.04","start":"36.48"},{"content":"PREPARE TO BATTLE ON A BOAT","dur":"1.48","start":"37.68"}]}}
Code
try {
JSONObject jsonObject = new JSONObject("transcript");
JSONArray jsonArray = new JSONArray("text");
Log.w("aaa",String.valueOf(jsonArray.getJSONObject(0).get("content")));
} catch (JSONException e) {
e.printStackTrace();
}
Put your json into a String and then try to parse it :
String json = "{\"transcript\":{\"text\":[{\"con ...}";
try {
JSONObject jsonObject = new JSONObject(json);
JSONObject jsonObject = new JSONObject("transcript");
JSONArray jsonArray = new JSONArray("text");
Log.w("aaa",String.valueOf(jsonArray.getJSONObject(0).get("content")));
} catch (JSONException e) {
e.printStackTrace();
}
Remember to escape your double quotes with backslash
From your question and code you've provided, I understand that you are new and unfamiliar with the JSON APIs in Android. For this, I suggest you read the official documentation and get yourself familiar with org.json API (JSONObject, JSONArray):
https://developer.android.com/reference/org/json/JSONObject
https://developer.android.com/reference/org/json/JSONArray
After, you may want to practice and follow some tutorials on serialization/deserialization of JSON in Android using the org.json API. Here is a fairly complete tutorial I just found for you on the net (just an example, there are literally hundreds out there):
https://www.javacodegeeks.com/2013/10/android-json-tutorial-create-and-parse-json-data.html
String response = "{"transcript":{"text":[{"content":"Flood!","dur":"1.76","start":"2.28"},{"content":"Listen up, there’s a flood coming!","dur":"2.24","start":"4.52"},{"content":"Oy lads, flood!","dur":"1.92","start":"7.88"},{"content":"There’s a flood –","dur":"1.24","start":"10.72"},{"content":"Maurice, they’re not listening.","dur":"1.84","start":"13.96"},{"content":"We’ve got to give them\na good old-fashioned fish slappin’.","dur":"2.8","start":"15.96"},{"content":"Fish Slap!","dur":"1.04","start":"20.52"},{"content":"Fish Slap!","dur":"1","start":"22.32"},{"content":"Fish Slap!","dur":"1.2","start":"24.12"},{"content":"Double Fish Slap!","dur":"1.24","start":"25.84"},{"content":"See, guys? Flood’s here.","dur":"1.56","start":"27.84"},{"content":"Tub Rider!","dur":"2.48","start":"31.56"},{"content":"SEASON 1\nTHE FLOOD","dur":"1","start":"35.32"},{"content":"Because I’m in a tub!","dur":"1.04","start":"36.48"},{"content":"PREPARE TO BATTLE ON A BOAT","dur":"1.48","start":"37.68"}]}}"
try {
JSONObject jsonObject = new JSONObject(response);
JSONObject transcript = jsonObject.getJSONObject("transcript")
JSONArray jsonArray = transcript.getJSONArray("text");
Log.w("aaa",String.valueOf(jsonArray.getJSONObject(0).get("content")));
} catch (JSONException e) {
e.printStackTrace();
}
You need to pass json string to Json object first.
please refer the below code, it might work for you.
try {
JSONObject obj = new JSONObject("{\"transcript\":{\"text\":[{\"content\":\"Flood!\",\"dur\":\"1.76\",\"start\":\"2.28\"},{\"content\":\"Listen up, there�s a flood coming!\",\"dur\":\"2.24\",\"start\":\"4.52\"},{\"content\":\"Oy lads, flood!\",\"dur\":\"1.92\",\"start\":\"7.88\"},{\"content\":\"There�s a flood �\",\"dur\":\"1.24\",\"start\":\"10.72\"},{\"content\":\"Maurice, they�re not listening.\",\"dur\":\"1.84\",\"start\":\"13.96\"},{\"content\":\"We�ve got to give them\\na good old-fashioned fish slappin�.\",\"dur\":\"2.8\",\"start\":\"15.96\"},{\"content\":\"Fish Slap!\",\"dur\":\"1.04\",\"start\":\"20.52\"},{\"content\":\"Fish Slap!\",\"dur\":\"1\",\"start\":\"22.32\"},{\"content\":\"Fish Slap!\",\"dur\":\"1.2\",\"start\":\"24.12\"},{\"content\":\"Double Fish Slap!\",\"dur\":\"1.24\",\"start\":\"25.84\"},{\"content\":\"See, guys? Flood�s here.\",\"dur\":\"1.56\",\"start\":\"27.84\"},{\"content\":\"Tub Rider!\",\"dur\":\"2.48\",\"start\":\"31.56\"},{\"content\":\"SEASON 1\\nTHE FLOOD\",\"dur\":\"1\",\"start\":\"35.32\"},{\"content\":\"Because I�m in a tub!\",\"dur\":\"1.04\",\"start\":\"36.48\"},{\"content\":\"PREPARE TO BATTLE ON A BOAT\",\"dur\":\"1.48\",\"start\":\"37.68\"}]}}");
JSONObject transcriptObject = obj.getJSONObject("transcript");
Log.d("transcript object",transcriptObject.toString());
JSONArray textArray = transcriptObject.getJSONArray("text");
Log.d("text array",textArray.toString());
} catch (Exception e) {
e.printStackTrace();
}
You can use GSON library for json parsing.
Instead of String.valueOf(jsonArray.getJSONObject(0).get("content"))
you could use :
jsonArray.getJSONObject(0).getString("content")
{
"Query":"query",
"KMAQuery":"query",
"TotalCount":3,
"Data":[{"CollName":"kmdb_new",
"TotalCount":3,
"Count":3,
"Result":[{"title":"sampletitle",
"director":[{"directorNm":"name1","directorId":"00004544"}],
"nation":"nation1",
"company":"company1",
"genre":"genre1",
"kmdbUrl":"http://www.kmdb.or.kr/vod/vod_basic.asp?nation=K&p_dataid=01040",
"rating":[{"ratingMain":"Y","ratingDate":"19640717","ratingNo":"","ratingGrade":"","releaseDate":"","runtime":""}]]}
Here is my Json Data from OKHttp parsing.
Actually There is many same Result 2~3.
I want to parsing key name "title", "directorNm", "nation", "company", "ratingGrade" and set Model class.
How to parsing multiple Json Object and Array with Gson into Model class?
I'm finally going to use the recyclerview with model class.
If you tell me how to parsing "title" and "directorNm", I can do to rest.
For reference, I am using a AsyncTask, OKHttp, Gson etc.
If you don't understand my question or need code, please comment!
I need your help vigorously.
Here I am sharing the code settting response from Model(Pojo Class)
public void getResponse(JSONObject jsonObject){
try {
JSONObject responseJson = new JSONObject(jsonObject.toString());
JSONArray jsonArray = responseJson.getJSONArray("heroes");
for (int i = 0; i < jsonArray.length(); i++){
//getting the json object of the particular index inside the array
JSONObject jsonObject = jsonArray.getJSONObject(i);
YourPojoClass pojoObject = new YourPojoClass();
pojoObject.setName(jsonObject.getString("name"));
pojoObject.setImageurl(jsonObject.getString("imageurl"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
I am trying to parse this json.
However, it is not working ...
I want to parse expected_departure_time of all the buses such as 4 , 15, C1, 4A in departure
this is my code which is not working.
try{
String str = response.getString("departures");
JSONArray jsonArray = response.getJSONArray(str);
JSONObject bus = jsonArray.getJSONObject(0);
String four = bus.getString("expected_departure_time");
textView.append(four);
}catch (JSONException e){
e.printStackTrace();
}
JSON
https://transportapi.com/v3/uk/bus/stop/6090282/live.json?app_id=d7180b02&app_key=47b460aac35e55efa666a99f713cff28&group=route&nextbuses=yes
The error you're making is that you're considering "departures" as a JsonArray, which is not the case in your JSON example, it is a JsonObject (Which in my opinion is a poor way of constructing this Json).
Anyway, you will have to get all the JsonObjects inside the "departure" JsonObject by doing this:
try
{
String jsonString=response.toString();
JSONObject jObject= new JSONObject(jsonString).getJSONObject("departures");
Iterator<String> keys = jObject.keys();
while( keys.hasNext() )
{
String key = keys.next();
JSONArray innerJArray = jObject.getJSONArray(key);
//This is your example, you can add a loop here
innerJArray(0).getString("expected_departure_time");
}
}
catch (JSONException e)
{ e.printStackTrace(); }
If you need to use the transport API for other features, to convert JSON strings to Java objects, you can map automatically by using GSON.
Follow the instructions from Leveraging the gson library and map any response you want from this API.
I'm working on parsing some JSON in my android application, this is the code I started off with:
JSONObject jsonObject = **new JSONObject(result);**
int receivedCount = **jsonObject.getInt("CurrentCount");**
However this is causing it to error (The code that would error is surrounded with asterisks) in Android Studio, I tried using the suggestion feature which asked me if I want "Surround with try/catch" which would cause the app to crash when it launched.
This is the suggested code:
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(result);
} catch (JSONException e) {
e.printStackTrace();
}
int receivedCount = 0;
try {
receivedCount = jsonObject.getInt("CurrentCount");
} catch (JSONException e) {
e.printStackTrace();
}
This is the JSON I'm trying to pass:
[{"CurrentCount":"5"},{"CurrentCount":"0"},{"CurrentCount":"1002"}]
Thanks in advance!
J_Dadh I think first of all you should look through the documentation on how to use Json Parser which you can find in the following Link https://www.tutorialspoint.com/android/android_json_parser.htm
EXAMPLE JSON
{
"sys":
{
"country":"GB",
"sunrise":1381107633,
"sunset":1381149604
},
"weather":[
{
"id":711,
"main":"Smoke",
"description":"smoke",
"icon":"50n"
}
],
"main":
{
"temp":304.15,
"pressure":1009,
}
}
String YourJsonObject = "JsonString"
JSONObject jsonObj = new JSONObject(YourJsonObject);
JSONArray weather = jsonObj.getJSONArray("weather");
for (int i = 0; i < weather.length(); i++) {
JSONObject c = weather.getJSONObject(i);
String id = c.getString("id");
String main= c.getString("main");
String description= c.getString("description");
}
as you pasted your JSON you are using [{"CurrentCount":"5"},{"CurrentCount":"0"},{"CurrentCount":"1002"}]
-If we analyze this JSON,This JSON contains a JSON ARRAY [{"CurrentCount":"5"},{"CurrentCount":"0"},{"CurrentCount":"1002"}]
having 3 JSON Objects{"CurrentCount":"5"},{"CurrentCount":"0"},{"CurrentCount":"1002"}
-But when you are going to parse this JSON, you are accepting it as jsonObject = new JSONObject(result),but you should accept it asJSONArray jsonArray=new JSONArray(result);
and then you iterate a loop(e.g,for loop) on this jsonArray,accepting JSONObjects 1 by 1,then getting the values from the each JSONObject 1 by 1.
-1 more mistake in your JSON is that you are sending the strings as "5" but accepting it as getInt() that's not fair,you should send int to accept it as intas 5 (without double qoutes)
So you final JSON and code like this(as below)
JSON
[{"CurrentCount":5},{"CurrentCount":0},{"CurrentCount":1002}]
Code to Use this JSON
JSONOArray jsonArray = null;
try{
jsonArray = new JSONArray(result);
for(int i=0;i<jsonArray.length;i++){
JSONObject jsonObject=jsonArray[i];
receivedCount=jsonObject.getInt("CurrentCount");
}
}catch(JSONException e) {
e.printStackTrace();
}
This might be a simple question, so sorry for that. I'm trying to develop a radio app for my internet radio station. I'm using JSON format for the requests (Example JSON) to get information from my station. For example the "title" tag. I want to get this information.
My first try:
JSONObject data = response.getJSONObject("data");
songname.setText(data.getString("title"));
But the problem is that I cannot get this information. What can I do? Thanks in advance.
As your JSON do like this
JSONObject josnOBJ = new JSONObject(response);
JSONArray jArray = josnOBJ.getJSONArray("data");
JSONObject jsonData = null;
String title = null;
for (int i=0; i < jArray.length(); i++)
{
try {
jsonData = jArray.getJSONObject(i);
title= jsonData .optString("title");
} catch (JSONException e) {
// Oops
}
}
songname.setText(title);
You could use some generic JSON parsers instead of manual work. Eg: https://github.com/bajicdusko/AndroidJsonProvider
Just create model class regarding your json content. Check examples in README on github url.
Hope i could help.
You can use GSON library in case you have lot of values to fetch from JSON ,that would take care of everything for you.