简体   繁体   中英

How to store json data in an Arraylist?

I have the following code where I am storing the json data from Elasticsearch in a file stored in my local drive. Instead, I want to store the data in an Arraylist, so I can pass that to the UI to display in front end.

try {           
    HttpGet request = new HttpGet("http://localhost:9200/indexname/_search?pretty=1&size=100&q=name:%22Test%22");             
    HttpResponse resp = client.execute(request);
    HttpEntity entity = resp.getEntity();

    byte[] buffer = new byte[1024];
    if (entity != null) {
        InputStream input = entity.getContent();
        FileWriter file = new FileWriter("/localpath/data.json");

        try {
            int bytesRead = 0;
            BufferedInputStream in = new BufferedInputStream(input);

            while ((bytesRead = in.read(buffer)) != -1) {
                String chunk = new String(buffer, 0, bytesRead);
                System.out.println(chunk);
                file.write(chunk);                  
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try { file.close(); inputStream.close(); } catch (Exception ignore) {}
        }               
    }
}

The JSON data is of the format:

{
    "took": 9,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 6.3519955,
        "hits": [
            {
                "_index": "indexname",
                "_type": "status",
                "_id": "eb825358-93d7-4a2f-9227-9b060cbc323a",
                "_score": 6.3519955,
                "_source": {
                    "name": "Test",
                    "ID": "ABCD",
                    "Number": "23222",
                    "SecCode": "124"
                }
            },
            {
                "_index": "indexname",
                "_type": "status",
                "_id": "eb825358-93d7-4a2f-9227-9b060cbc323b",
                "_score": 6.3519955,
                "_source": {
                    "name": "Test",
                    "ID": "CDEF",
                    "Number": "5678",
                    "SecCode": "120"
                }
            }
        ]
    }
}

In my arraylist, I want to store just the ID's and Number's.

Any help would be appreciated? Thanks

public static List<Pattern> pattern(){
    ArrayList<Pattern> patterns = new ArrayList<>();
    Gson gson = new Gson();
    JsonParser jsonParser = new JsonParser();
    try {
        BufferedReader bufferedReader  = new BufferedReader(new FileReader("MyJsons/objects.json"));
        JsonElement jsonElement = jsonParser.parse(bufferedReader);

        //Create generic type
        Type type = new TypeToken<List<Pattern>>() {}.getType();
        return gson.fromJson(jsonElement, type);

    } catch (IOException e) {
        e.printStackTrace();
    }
    return patterns;        
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM