Saturday 16 March 2019

Create JSONArray from JSON String


By passing JSON String to the JSONArray constructor, you can construct JSONArray from the string.

Example
JSONArray jsonArray = new JSONArray(jsonString);

App.java
package com.sample;

import org.json.JSONArray;

public class App {

 public static void main(String args[]) {
  String jsonString = "[{\"organization\":\"ABC Corporation\",\"name\":\"Krishna\",\"id\":1},{\"organization\":\"XYZ Corporation\",\"name\":\"Rama\",\"id\":2},{\"organization\":\"TUV Corporation\",\"name\":\"Chamu\",\"id\":3}]\n";
  JSONArray jsonArray = new JSONArray(jsonString);

  System.out.println(jsonArray.get(0));
  System.out.println(jsonArray.get(1));
  System.out.println(jsonArray.get(2));

 }
}

Output
{"organization":"ABC Corporation","name":"Krishna","id":1}
{"organization":"XYZ Corporation","name":"Rama","id":2}
{"organization":"TUV Corporation","name":"Chamu","id":3}



Previous                                                 Next                                                 Home

No comments:

Post a Comment