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")
Related
{
"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.
It is possible to retrieve a JSON object without a key name?
One more problem is that it is deep in the hierarchy. Please see this: http://jsonviewer.stack.hu/#http://gateway.marvel.com/v1/public/characters?apikey=2d0af97a020cd072d49059aa0bf13207&hash=ef7184ddbb03ed2f71da0efec112cf41&ts=1495035369
That is an intensively long JSON and has multiple objects.
I am trying to access this part of the JSON: {
"id": 1010699,
"name": "Aaron Stack", ..
I am using the following code:
private void parseResult(String result) {
try {
JSONObject response = new JSONObject(result);
JSONArray posts = response.optJSONArray("data");
for (int i = 0; i < posts.length(); i++) {
JSONObject post = posts.optJSONObject(i);
String title = post.optString("results");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
But I can't access it.
You can use GSON for this
compile 'com.google.code.gson:gson:2.7'
and create POJO class for the corroesponding json result using this link, Just paste your json data and create classes.
For eg your main pojo class name be JsonResponseHolder
And in your java code
try {
JsonResponseHolder jrh = new Gson().fromJson(responseString,
JsonResponseHolder.class);
List<Results> results = jrh.getData().getResults();
/// this will give the result objects
/// and be sure to convert [] arrays to list for better data handling
} catch (JSONException e) {
e.printStackTrace();
}
Hope this will help
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.
I am writing an android application and currently i am getting a JSON reply:
{"Error":"User already exists"}
this is an example of the messages that i get returned
the part that i am after is : "User already exists" and i need to parse it.
The message is currently stored in a string so i would need to convert this to a JSONArray to then convert it to a JSONOject to then be able to call the getString().
what would be the best way to do this?
this may Helps You
String Respones= "{\"Error\":\"User already exists\"}";
try {
JSONObject jobj = new JSONObject(Respones);
String strError = jobj.getString("Error");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I think you can do it directly with JSONTokener and JSONObject if you want ot use default andoid tools.
If you want better, look into the Jackson lib, it's very powerful, open source, kind of standard:
Android JSON Jackson Tutorial
Try this code
JSONArray jsonArray = new JSONArray(ResponseString);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String result = jsonObject.getString("Error").toString().trim();
}