For a long time, I often encounter json data from the front end or the data I often crawl from the website, there will be json data, so if you want to get json data, you need to parse the json data
Below I use java language to parse json data
A package needs to be introduced before parsing the data
You can download the corresponding jar package from http://mvnrepository.com/artifact/edu.uci.ics here
public class JsonToObject {
/**Parse the case where the outermost part of the json string is []
* [{"rating":["9.6","50"],"rank":1},{"rating":["9.6","50"],"rank":2}]
* @throws Exception
*/
public List<Map<String, String>> toMapOne(String jsonString)throws Exception{
//The first step is to parse and remove the outermost layer of the array to get all the json strings
JSONArray jsons = JSONArray.fromObject(jsonString);
List<Map<String, String>> nodes = new ArrayList<Map<String, String>>();
//loop through all json substrings
for (Object o : jsons)
{
JSONObject jsonNode = JSONObject.fromObject (o);
Map<String, String> treeNodes = new HashMap<String, String>();
String rating = "";
JSONArray ratingArray = JSONArray.fromObject(jsonNode.getString("rating").trim());
for(Object ob : ratingArray) {
rating += ob.toString()+",";
}
treeNodes.put("rating",rating);
treeNodes.put("rank",jsonNode.getString("rank").trim());
nodes.add(treeNodes);
}
return nodes;
}
/**Parse the case where the outermost part of the json string is {}
* {"data":[{"directors":["Putepong Prosaka na Sakna Kalin","Watson Pokpen"],"rate":"8.3"}, {"directors":["David Fincher"],"rate":"8.7"}]}
* @throws Exception
*/
public List<Map<String, String>> toMapTwo(String jsonString)throws Exception{
//First convert the jsonString string to jsonObject
JSONObject jsonNode = JSONObject.fromObject(jsonString);
//Then get the value of data in json
JSONArray jsonArray = JSONArray.fromObject(jsonNode.getString("data"));
List<Map<String, String>> nodes = new ArrayList<Map<String, String>>();
// loop through the values in data
for (Object object : jsonArray) {
JSONObject jsonNode2 = JSONObject.fromObject(object);
Map<String, String> treeNodes = new HashMap<String, String>();
treeNodes.put("directors",jsonNode2.getString("directors"));
treeNodes.put("rate",jsonNode2.getString("rate").trim());
nodes.add(treeNodes);
}
return nodes;
}
public static void main(String[] args) throws Exception {
String jsonString = "[{\"rating\":[\"9.6\",\"50\"],\"rank\":1},{\"rating\":[\"9.6\",\"50\"],\"rank\":2}]";
String jsonString2 = "{\"data\":[{\"directors\":[\"Putepong Prosaka na Saknakarin\",\"Watson Pokpen\" ],\"rate\":\"8.3\"},{\"directors\":[\"David Fincher\"],\"rate\":\"8.7\"}]}";
JsonToObject jo = new JsonToObject();
List<Map<String, String>> listMap = jo.toMapOne(jsonString);
List<Map<String, String>> listMap2 = jo.toMapTwo(jsonString2);
//The json data is obtained above and encapsulated into the list collection, and the list
//It encapsulates the map collection
for (Map<String, String> map : listMap) {
String rating = map.get("rating");
String rank = map.get("rank");
System.out.println("rating: "+rating);
System.out.println("rank: "+rank);
}
for (Map<String, String> map : listMap2) {
String directors = map.get("directors");
String rate = map.get("rate");
System.out.println("directors: "+directors);
System.out.println("rate: "+rate);
}
}
}
Result of running:
For the variable value situation of function debugging in toMapOne: