How to use JSON to get value? - java

Log.d("Hot Text:", response.toString());
try {
JSONArray array = response.getJSONArray("rows");
JSONObject jsonObject = array.getJSONObject(0);
JSONArray a = jsonObject.getJSONArray("elements");
JSONObject js = a.getJSONObject(0);
JSONObject b = js.getJSONObject("distance");
value = b.getString("value");
Log.d("TAG", "title:" + value);
} catch (JSONException e) {
e.printStackTrace();
}
}
how to get "value" to storage for other space? thank u

You can do like this:
String text;
int value;
try {
JSONObject jsonObject = new JSONObject(body);
JSONArray rowArray = jsonObject.optJSONArray("rows");
if(rowArray.length() > 0){
JSONObject row = rowArray.optJSONObject(0);
JSONArray elementArray = row.optJSONArray("elements");
if(elementArray.length() > 0){
JSONObject element = elementArray.optJSONObject(0);
JSONObject distance = element.optJSONObject("distance");
text = distance.optString("text");
value = distance.optInt("value");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Log.e("text", text);
Log.e("value", "value : " + value);

Related

how do I extract the JSON value?

I am getting and error response from server in android studio. how do I extract the values.
Json:
{
"error": {
"phone": [
"The phone field is required."
]
}
}
Code:
try {
JSONArray arr = new JSONArray(response);
for (int i = 0; i < arr.length(); i++) {
JSONObject mJsonObject = arr.getJSONObject(i);
Log.d("OutPut", mJsonObject.getString("phone"));
}
} catch (JSONException e) {
e.printStackTrace();
}
Actually your response is an object not an array. Try below:
String response = "{\"error\":{\"phone\":[\"The phone field is required.\"]}}";
try {
JSONObject jsonObject = new JSONObject(response);
JSONObject errorObject = jsonObject.optJSONObject("error");
JSONArray phoneArray = errorObject.getJSONArray("phone");
for (int i = 0; i < phoneArray.length(); i++) {
String errorString = phoneArray.optString(i);
Log.d("OutPut", errorString);
}
} catch (JSONException e) {
e.printStackTrace();
}

Parse json string to json array? [duplicate]

This question already has answers here:
Convert string to JSON array
(10 answers)
Closed 5 years ago.
i need help to convert json string to json array as i given below
{"level":[{"id":"1","name":"first","time":"00:02:00"},{"id":"2","name":"second","time":"00:03:00"},{"id":"3","name":"math","time":"00:03:00"},
{"id":"4","name":"language ","time":"00:03:00"},{"id":"5","name":"sport","time":"00:04:00"}]}
to use it in android studio
Try this
try
{
JSONObject resObject = new JSONObject("your json response");
JSONArray jsonArray = resObject.getJSONArray("level");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Log.i("id", "" + jsonObject.getString("id"));
Log.i("name","" + jsonObject.getString("name"));
Log.i("time", "" + jsonObject.getString("time"));
}
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
OUTPUT
Try this
try {
JSONObject jsonObject=new JSONObject(yourresponsestring);
JSONArray jsonArray=new JSONArray(jsonObject.getJSONArray("level"));
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1=jsonArray.getJSONObject(i);
String id=jsonObject1.getString("id");
String name=jsonObject1.getString("name");
String time=jsonObject1.getString("time");
}
} catch (JSONException e) {
e.printStackTrace();
}
You can try below code to convert the data to JSON:
try {
JSONObject jsonObject = new JSONObject("{\"level\":[{\"id\":\"1\",\"name\":\"first\",\"time\":\"00:02:00\"},{\"id\":\"2\",\"name\":\"second\",\"time\":\"00:03:00\"},{\"id\":\"3\",\"name\":\"math\",\"time\":\"00:03:00\"}, {\"id\":\"4\",\"name\":\"language \",\"time\":\"00:03:00\"},{\"id\":\"5\",\"name\":\"sport\",\"time\":\"00:04:00\"}]}");
JSONArray level = jsonObject.getJSONArray("level");
for (int i = 0; i < level.length(); i++) {
JSONObject data = level.getJSONObject(i);
String id = data.getString("id");
String name = data.getString("name");
String time = data.getString("time");
Log.d("JSONDATA--->", "Data at: " + i + " " + id + ":" + name + ":" + time);
}
} catch (JSONException e) {
e.printStackTrace();
}

android JSONException index 1 out of range [0..1] (Parse 2 json arrays inside 1 loop)

I have code like this, the value of jArrAnswer is
[{"answer":"Yes"},{"answer":"No"},{"answer":"maybe"},{"answer":"yrg"}]
the result from jArrAnswer.length() is 4
but why I got error
org.json.JSONException: Index 1 out of range [0..1).
try {
JSONArray jArrAnswerid = new JSONArray(answerid);
JSONArray jArrAnswer = new JSONArray(answer);
for (int i = 0; i < jArrAnswer.length(); i++) {
JSONObject jObjAnswerid = jArrAnswerid.getJSONObject(i);
JSONObject jObjAnswer = jArrAnswer.getJSONObject(i);
String ansid = jObjAnswerid.getString("answerid");
String ans= jObjAnswer.getString("answer");
GroupModel item2 = new GroupModel(String.valueOf(i + 1), ans, ansid);
}
} catch (Exception e) {
Log.w("asdf", e.toString());
}
You are iterating the for loop over jArrAnswer while your fetching the index i over jArrAnswerid.
Check and make sure that the jArrAnswerid.size() is equal to the jArrAnswer.size().
Print the jArrAnswerid.size() and check.
Try this
try {
JSONArray jArrAnswer = new JSONArray(answer);
for (int i = 0; i < jArrAnswer.length(); i++) {
JSONObject jObjAnswer = jArrAnswer.getJSONObject(i);
String ansid = jObjAnswer.getString("answerid");
String ans= jObjAnswer.getString("answer");
}
} catch (Exception e) {
Log.w("asdf", e.toString());
}
provided "answer" is your json array response
Try
String json = "[{\"answer\":\"Yes\",\"answerid\":\"1\"},{\"answer\":\"No\",\"answerid\":\"2\"},{\"answer\":\"maybe\",\"answerid\":\"3\"},{\"answer\":\"yrg\",\"answerid\":\"4\"}]";
try {
JSONArray jsonArray = new JSONArray(json);
if(jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String answerId = jsonObject.getString("answerid");
String answer = jsonObject.getString("answer");
//Use answerId and answer
}
}
} catch(JSONException e) {
e.printStackTrace();
}

How to read Json Android

i so confused ! i'm so trying to read this json but i can't :-(
here is my code :
{"status":"loged_in","token":"8e88776a14f4da4ef8e00955f83e1397","nikeName":"سعید"}
try {
postTextandGetRespons("http://gfac.ir/KatibehPayam/Service/login.php");
JSONArray messages = new JSONArray(responseString);
for ( int i=0; i<= messages.length();i++){
JSONObject c = messages.getJSONObject(i);
Status[0] = c.getString("status");
Status[1] = c.getString("token");
Status[2] = c.getString("nikeName");
}
} catch (JSONException e) {
e.printStackTrace();
}
Your responseString contains an object not an array so use JSONObject instead of JSONArray
JSONObject message = new JSONObject(responseString);
String status = message.getString("status");
String token = message.getString("token");
String nikeName = message.getString("nikeName");

Parsing a flat JSON array with no indice

I am scratching my head to figure out how to parse the following JSON object.
[
{
"result": true,
"response": "Successfully got list of users in radius of 10km"
},
{
"username": "elize",
"photo": "http://www.embedonix.com/apps/mhealth/images/elize/elize.png"
},
{
"username": "mario",
"photo": "http://www.embedonix.com/apps/mhealth/images/mario/mario.png"
}
]
This is I guess a single index json array. The first part tells that the operation of building the json object was ok.
Then there are pairs of username and photo which I have to parse them and put them in a list:
public class User {
private String mName;
private String mPhotoURl;
public User(String name, String url)
{
///
}
}
So if the first entry of json is result -> true I should have a ArrayList<User>.
I tried to do the following but it always raises the JSONParse exception:
try {
JSONObject json = new JSONObject(data);
int length = json.length();
for (int i = 0; i < length; i++) {
JSONArray obj = json.getJSONArray(String.valueOf(i));
Log.i(TAG, i + " username: " + obj.getString(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
Just try this way
try {
JSONArray jsonArray = new JSONArray(data);
int length = jsonArray.length();
for (int i = 1; i < length; i++) {
JSONObject obj = jsonArray.getJSONObject(i);
Log.i(TAG, i + " username: " + obj.getString("username"));
Log.i(TAG, i + " photo: " + obj.getString("photo"));
}
} catch (JSONException e) {
e.printStackTrace();
}
Try this way,hope this will help you to solve your problem.
try {
JSONArray jsonArray = new JSONArray(data);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
if(i==0){
Log.i(TAG, i + " result: " + obj.getString("result"));
Log.i(TAG, i + " response: " + obj.getString("response"));
}else{
Log.i(TAG, i + " username: " + obj.getString("username"));
Log.i(TAG, i + " photo: " + obj.getString("photo"));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Try Code...something like this..
try {
JSONArray json = new JSONArray(data);
int length = json.length();
for (int i = 0; i < length; i++) {
JSONObject childObject = json.getJSONObject(i);
if(i==0){
String result = child.getBoolean("result");
String resp = child.getString("response");
} else {
String username = child.getString("username");
String photo = child.getString("photo");
Log.i(TAG, i + " username: " + username);
}
}
} catch (JSONException e) {
e.printStackTrace();
}

Categories