-2

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
  • 37,901
  • 21
  • 84
  • 115
Elia Ibrahem
  • 77
  • 1
  • 1
  • 10
  • i try to use jsonarray(string) but it return json exception – Elia Ibrahem Jan 01 '18 at 13:28
  • please check my ans. – Ratilal Chopda Jan 01 '18 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 [mcve]. – Jörg W Mittag Jan 01 '18 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 [mcve]. – Jörg W Mittag Jan 01 '18 at 13:41

3 Answers3

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

enter image description here

Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
1

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();
    }
Ramesh sambu
  • 3,577
  • 2
  • 24
  • 39
1

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();
}
Pawan Dubey
  • 192
  • 3
  • 15