09 June 2017

API for JSON (JSR 353): Convert "JSON String or JSON String file" To ArrayList

String Json To ArrayList
Java Json 2017
package com.json;

import java.util.ArrayList;
import java.util.List;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class StringJsonToArrayList {

      public static void main(String[] args) {
            // Json String
            String str = "[{\"user_id\": \"someValue_1\"},{\"user_id\": \"someValue_2\"},{\"user_id\": \"someValue_3\"}]";

            // import json-simple-1.1.jar
            JSONParser parser = new JSONParser();
            JSONArray jsonArray = new JSONArray();
            List<String> list = new ArrayList<String>();

            try {
                 jsonArray = (JSONArray) parser.parse(str);
            } catch (Exception e) {
                 System.out.println(e.toString());
            }

            for (int i = 0; i < jsonArray.size(); i++) {
                 JSONObject obj = (JSONObject) jsonArray.get(i);
                 System.out.println(obj.get("user_id").toString());
                 list.add(obj.get("user_id").toString()); // Save Arraylist
            }

            for (int i = 0; i < list.size(); i++) {
                  System.out.println(list.get(i));
            }
      }
}
Output:
someValue_1
someValue_2
someValue_3
someValue_1
someValue_2
someValue_3
File Json To ArrayList
Java Json 2017
package com.json;

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class FileJsonToArrayList {

      public static void main(String[] args) {

            // import json-simple-1.1.jar
            JSONParser parser = new JSONParser();
            JSONArray jsonArray = new JSONArray();
            List<String> list = new ArrayList<String>();

            try {
                  jsonArray = (JSONArray) parser.parse(new InputStreamReader(new FileInputStream("C:\\Users\\Lonely-Cat\\workspace\\Demo01\\src\\com\\json\\jsonTestFile.json")));
            } catch (Exception e) {
                 System.out.println(e.toString());
            }

            for (int i = 0; i < jsonArray.size(); i++) {
                 JSONObject obj = (JSONObject) jsonArray.get(i);
                 System.out.println(obj.get("user_id").toString());
                 list.add(obj.get("user_id").toString()); // Save Arraylist
            }

            for (int i = 0; i < list.size(); i++) {
                 System.out.println(list.get(i));
            }
      }
}
File: jsonTestFile.json
[
       {
              "user_id": "someValue_1"},
       {
              "user_id": "someValue_2"},
       {
              "user_id": "someValue_3"
       }
]
Note json:
[] <-- Array
{} <-- Object
Output:
someValue_1
someValue_2
someValue_3
someValue_1
someValue_2
someValue_3

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang