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 need to get the value of idinside studentarray. The response I get is,
{
"response": {
"student": [
{
"id": "125745",
"module": 3,
"status": 1
}
]
}
}
I tried using following code,
String userId = null;
try {
JSONObject object = (JSONObject) new JSONTokener(response).nextValue();
userId= object.getString("id");
} catch (JSONException e) {
e.printStackTrace();
}
But it doesn't work. How do I retrieve id?
You are almost there, just you need to do this:
JSONArray students = object.getJSONArray("student");
JSONObject student = students.getJSONObject(0);
userId= student.getString("id");
Because the id value is placed in a JSONObject, then inside a JSONArray at index 0, then it is again placed inside a JSONObject.
Also don't forget to handle exceptions, the code above, is just for your understanding.
Hope that helps!!
Your value is placed in json array. So, you need to retrieve response object using getJSONObject and then get student json array via getJSONArray. Then you will be able to iterate through student objects. There is no way to magically get id from json.
Alternatively, you can map your json to Java objects using Gson, for example.
Try this :
Let all of the json is called
String serverResponse = "Response from the server";
try {
JSONObject object = new JSONObject(serverResponse);
String userId = object.getJSONObject("response").getJSONArray("student").getJSONObject(0).getString("id");
}
catch (JSONException e) {
e.printStackTrace();
}
Hope this helps.
Assuming jsonObject is a reference to your root json, you can get id of the first student:
JSONObject response = (JSONObject) jsonObject.get("response");
JSONArray students = (JSONArray) response.get("student");
int id = (int) ((JSONObject)students.get(0)).get("id");
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();
}
{
"status": "ERROR",
"msg_content": "Your old password was entered incorrectly. Please enter it again.",
"code": "400",
"msg_title": "Sorry. Error in processing your request"
}
if (str.startsWith("Bad Request"))
{
textview.setText(" ");
}
How to print inside the textview to display the msg_content using json
You need to parse json using JSON object.
JSONObject obj = new JSONObject(str);
Then find string from JSON object whatever you want.
if (obj.has("msg_content")) {
String value = obj.getString("msg_content");
textview.settext(value);
}
You will need to create a JSONObject and pass it the string as a parameter.
JSONObject obj = new JSONObject(str);
Then to find a key in the JSONObject just call, always check if the JSONObject has that key before trying to retrieve it.
if (obj.has("status"){
String status = obj.getString("status");
}
if (obj.has("msg_content"){
String content = obj.getString("msg_content");
textview.setText(content);
}
JsonReader is implemented in API 11. If you want to use it in GingerBread or below try this
Use following Code for extracting Information from JSON Objects:
try {
Iterator keys = jsonObject.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
if(key.equals("msg_content"))
textView.setText(jsonObject.getString(key));
}
} catch (JSONException e) {
e.printStackTrace();
}
Also, if u have JSON as String, u can populate an object using following code:
try {
jsonObject = new JSONObject(theJsonString);
} catch (JSONException e1) {
e1.printStackTrace();
}
I'm quite new to JSON but I would create a JsonReader and parse the JSON as per here