Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.text.MaskFormatter;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- public class MainTest {
- public static void main(String[] args){
- //makeJson();
- }
- public static void makeJson(){
- String name = "prashun", age="25";
- String name2 = "palash", age2="26";
- JSONObject obj = new JSONObject();
- try {
- obj.put("name",name);
- obj.put("age",age);
- } catch (JSONException e) {
- System.out.println(e.getMessage());
- }
- JSONArray objA = new JSONArray();
- objA.put(obj);
- obj = new JSONObject();
- try {
- obj.put("name",name2);
- obj.put("age",age2);
- } catch (JSONException e) {
- System.out.println(e.getMessage());
- }
- objA.put(obj);
- getJson(objA); // passing the JSONArray to another method
- }
- public static void getJson(JSONArray objA){
- String name = "";
- String age = "";
- JSONObject obj = new JSONObject();
- for(int i=0;i<objA.length();i++){
- try {
- obj = objA.getJSONObject(i);
- name = obj.getString("name");
- age = obj.getString("age");
- } catch (JSONException e) {
- System.out.println(e.getMessage());
- }
- }
- System.out.println(objA.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement