Read json string or custom json string from json file and convert it to object. The object used below is map. According to the map, you can read a certain data of json, and you can read the first level of data name. Later, I found that I wanted to convert it to JsonArray to read "red", so I had to use other methods.
Finally, I used the org.json package to solve it (readJsonArray function). I will see if there is a better solution when I have time.
The JSON file is as follows:
{ "name":"name", "id":"id", "color":[ {"red":"red","blue":"blue"}, {"white":"white"} ]}The code is as follows:
package com;import org.codehaus.jackson.map.ObjectMapper;import org.json.JSONArray;import org.json.JSONObject;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.*;import java.util.Map;/** * Hello world! * */public class JsonAnalysis{ private static final Logger LOG = LoggerFactory.getLogger(JsonAnalysis.class); public static void main(String[] args) throws FileNotFoundException { String jsonString = "{/"address/":/"address/",/"name/"name/":/"name/",/"id/":/"1/",/"email/":/"email/"}"; FileReader fileReader = new FileReader("E://JsonAnalysis//src//test.json"); String fileString = readFile(fileReader); //Json string is converted to a java object, such as converting to a Map object to read the data Map map = null; Map mapFile = null; try { map = readValue(jsonString, Map.class); mapFile = readValue(fileString, Map.class); } catch (Exception e) { e.printStackTrace(); LOG.error("ReadValue occurs exception when switch json string to map"); } System.out.println(map != null ? map.get("id") : null); if (mapFile==null){ LOG.info("Json map form file is empty"); return; } System.out.println(mapFile.get("name")); try { readJsonArray(fileString); } catch (Exception e) { e.printStackTrace(); } } //Json string to object private static <T> T readValue(String jsonStr, Class<T> valueType) throws Exception{ ObjectMapper objectMapper = new ObjectMapper(); try {// Object object = objectMapper.readValue(jsonStr,Object.class); return objectMapper.readValue(jsonStr,valueType); } catch (IOException e) { e.printStackTrace(); } return null; } //Read file and to string private static String readFile(FileReader fileReader){ BufferedReader bufferedReader = new BufferedReader(fileReader); StringBuilder fileStr = new StringBuilder(); try { String eachLine; while ((eachLine=bufferedReader.readLine())!=null){ fileStr.append(eachLine); } return fileStr.toString(); } catch (IOException e1) { e1.printStackTrace(); LOG.error("Occur exception when read file,file={}",fileReader); return null; } } //Fetch json array according to json string and read data (note that this part refers to the org.json package) private static void readJsonArray(String jsonStr) throws Exception { JSONObject jsonObject = new JSONObject(jsonStr); JSONArray jsonArray = jsonObject.getJSONArray("color"); JSONObject jsonObject1 = jsonArray.getJSONObject(0); System.out.println(jsonObject1.get("red")); }}The above example of Jackson converting json string to Object, org.json to read json array is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.