标签:eva 子节点 img 技术 ESS static obj dtree res
一、Json 报文格式如下:
二、获取 Json 报文中字段的内容
1 import java.io.IOException; 2 import com.fasterxml.jackson.core.JsonProcessingException; 3 import com.fasterxml.jackson.databind.JsonNode; 4 import com.fasterxml.jackson.databind.ObjectMapper; 5 6 public class ReadJsonValue_1_00_001 { 7 8 public static void main(String args[]) { 9 10 String test = "{\n" + 11 " \"results\":\n" + 12 " [\n" + 13 " {\n" + 14 " \"objectID\":\"357\",\n" + 15 " \"geoPoints\":\n" + 16 " [\n" + 17 " {\n" + 18 " \"x\":504604.59802246094,\n" + 19 " \"y\":305569.9150390625\n" + 20 " }\n" + 21 " ]\n" + 22 " },\n" + 23 " {\n" + 24 " \"objectID\":\"358\",\n" + 25 " \"geoPoints\":\n" + 26 " [\n" + 27 " {\n" + 28 " \"x\":504602.2680053711,\n" + 29 " \"y\":305554.43603515625\n" + 30 " }\n" + 31 " ]\n" + 32 " }\n" + 33 " ]\n" + 34 "}\n"; 35 36 System.out.println( test ); 37 38 ObjectMapper mapper = new ObjectMapper(); 39 JsonNode node = null; 40 try { 41 node = mapper.readTree( test ); 42 } catch (JsonProcessingException e) { 43 e.printStackTrace(); 44 } catch (IOException e) { 45 e.printStackTrace(); 46 } 47 48 //将Json串以树状结构读入内存 49 JsonNode results = node.get("results");//得到results这个节点下的信息 50 System.out.println( "results.size() = " + results.size() ); 51 52 for(int i = 0; i < results.size(); i++ ) //遍历results下的信息,size()函数可以得节点所包含的的信息的个数,类似于数组的长度 53 { 54 System.out.println( results.get(i).get("objectID").textValue() ); //读取节点下的某个子节点的值 55 56 JsonNode geoNumber = results.get(i).get("geoPoints"); 57 System.out.println( "geoNumber() = " + geoNumber.size() ); 58 for(int j = 0; j < geoNumber.size(); j++ ) //循环遍历子节点下的信息 59 { 60 System.out.println(geoNumber.get(j).get("x").doubleValue()+"--"+geoNumber.get(j).get("y").doubleValue()); 61 } 62 } 63 } 64 65 }
三、读取结果为
标签:eva 子节点 img 技术 ESS static obj dtree res
原文地址:https://www.cnblogs.com/dgm2088/p/10000380.html