Andorid-json与原始解析

xiaoxiao2021-02-28  2

1.json
   json的格式  2种格式 对象{ } 数组[ ] { name:'xiaoli', age:'18', sex:'nv',} <?xml version="1.0" encoding="UTF-8"?> <student>         <name>小红</name>         <age>29</age>         <sex></sex>     </student> 总结:json格式更易于阅读,更轻量,更容易解析.
2.json解析
public class JsonParserTest { @Test public void parserJsonTest1() throws Exception{ //1.模拟json串 String testJson = "{name:'xiaoli',age:'18',sex:'nv'}"; //2.创建jsonobject对象 JSONObject jsonObject = new JSONObject(testJson); //3.根据键取到对应的值 String name = (String) jsonObject.get("name"); String age = (String) jsonObject.get("age"); System.out.println(name+"--"+age); } @Test public void parserJsonTest2() throws Exception{ //1.模拟json串 String testJson = "[{name:'xiaoli',age:'18',sex:'男'},{name:'xiaohong',age:'28',sex:'nv'}]"; //2.创建jsonarray对象 JSONArray jsonArray = new JSONArray(testJson); JSONObject jsonObject = (JSONObject) jsonArray.get(1); //3.根据键取到对应的值 String name = (String) jsonObject.get("name"); String age = (String) jsonObject.get("age"); System.out.println(name+"--"+age); } }
转载请注明原文地址: https://www.6miu.com/read-1600286.html

最新回复(0)