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
Phantômaxx
38.1k21 gold badges88 silver badges122 bronze badges
asked Jan 1, 2018 at 13:25
Elia Ibrahem
771 gold badge1 silver badge10 bronze badges
-
i try to use jsonarray(string) but it return json exceptionElia Ibrahem– Elia Ibrahem2018年01月01日 13:28:45 +00:00Commented Jan 1, 2018 at 13:28
-
please check my ans.Ratilal Chopda– Ratilal Chopda2018年01月01日 13:34:22 +00:00Commented Jan 1, 2018 at 13:34
-
What is the code you are having trouble with? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? Please, provide a minimal reproducible example.Jörg W Mittag– Jörg W Mittag2018年01月01日 13:40:45 +00:00Commented Jan 1, 2018 at 13:40
-
Can you provide a precise specification of what it is that you want to happen, including any and all rules, exceptions from those rules, corner cases, special cases, boundary cases, and edge cases? Can you provide sample inputs and outputs demonstrating what you expect to happen, both in normal cases, and in all the exceptions, corner cases, special cases, boundary cases, and edge cases? Please, also make sure to provide a minimal reproducible example.Jörg W Mittag– Jörg W Mittag2018年01月01日 13:41:10 +00:00Commented Jan 1, 2018 at 13:41
3 Answers 3
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
answered Jan 1, 2018 at 13:31
Ratilal Chopda
4,2304 gold badges21 silver badges33 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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();
}
answered Jan 1, 2018 at 13:30
Ramesh sambu
3,5352 gold badges26 silver badges40 bronze badges
Comments
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();
}
answered Jan 1, 2018 at 13:44
Pawan Dubey
3446 silver badges16 bronze badges
Comments
lang-java