1.JSON 的语法规则是怎样的?
- 数组(Array)用方括号(“
[]
”)表示。 - 对象(0bject)用大括号(“
{}
”)表示。 - 名称/值对(
name/value
)组合成数组和对象。
- 名称(
name
)置于双引号中,值(value
)有字符串、数值、布尔值、null、对象和数组。 - 并列的数据之间用逗号(“
,
”)分隔
[{"sku":"859545","num":"1","m_price":"18.9","j_price":"14.9"}]
{ "name": "xdr630", "favorite": "programming" }
[1,2,"three","four",true,false,null,[1,2],{"name":"兮动人"}]
2.JAVA中json和各种类型的转换大全
阿里提供的JSON包实现json类型的互转
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.60</version> </dependency>
1. JAVA对象转JSON字符串
//java对象转json字符串 User user= new User(); user.setName("lq"); String s = JSON.toJSONString(data); System.out.println(s); //输出结果{"Name":"lq"}
2. JSON字符串转JSON对象
//json字符串转json对象 String s="{\"Name\":\"lq\"}"; JSONObject jsonObject = JSON.parseObject(s); String name= jsonObject.getString("Name"); System.out.println("name ="+name);//add System.out.println("jsonObject ="+jsonObject); //输出结果 //Name =lq //jsonObject ={"Name":"lq"}
3.JSON字符串转JAVA简单对象
String s ="{"Name":"lq"}"; User user= JSON.parseObject(s, User.class); System.out.println("user对象"+user.toString()); System.out.println("name="+user.getName()"); //data对象Data{"Name":"lq"} //Name=lq
4.JsonArray中添加json数据
//创建JSONObject JSONObject paramJson= new JSONObject(); paramJson.put("type","1"); paramJson.put("switchType","2"); //创建JSONArray JSONArray paramArray = new JSONArray(); //将JSONObject放入JSONArray中 paramArray.add(paramJson); JSONObject json = new JSONObject(); json.put("sysId", "sysId"); json.put("sign", "sign"); //将JSONArray放入JSONObject中 json.put("param", paramArray); System.out.println(json.toString()); //{"sysId":"sysId","param":[{"switchType":"2","type":"1"}],"sign":"sign"}
解析json实例
{ "success": true, "result": [ { "id": 652, "name": "数码", "pid": 0, "level": 1, "childs": [ { "id": 654, "name": "摄影摄像", "pid": 652, "level": 2 }, { "id": 828, "name": "影音娱乐", "pid": 652, "level": 2 } ] } ], "return_code": "", "return_msg": "" }
String lqcategoryparent ="要解析的json字符串"; JSONObject lqcategoryjson = JSON.parseObject(lqcategoryparent); JSONArray result = (JSONArray) JSONPath.eval(lqcategoryjson, "$.result"); //json数组遍历 result.forEach(lqcategoryobj->{ JSONObject infojo= (JSONObject) lqcategoryobj; String categoryid=infojo.getString("id"); String categoryName=infojo.getString("name"); String categoryLevel=infojo.getString("level"); String childs=infojo.getString("childs"); JSONArray childsjson = JSON.parseArray(childs); //获取到childs数组 //json数组遍历 childsjson.forEach(lqchilds->{ JSONObject lqchildsobj= (JSONObject) lqchilds; String categoryid2=lqchildsobj.getString("id"); String categoryName2=lqchildsobj.getString("name"); String categoryLevel2=lqchildsobj.getString("level"); }); });
{ "sn_responseContent":{ "sn_body":{ "queryProdImage":{ "resultInfo":[ { "skuId":"12310588118", "urls":[ { "path":"http://imgservice1.suning.cn/uimg1/b2c/image/VKitti0Sy9lhYde-D3Hh5w.jpg_800w_800h_4e", "primary":1, "pictureLocation":1 }, { "path":"http://imgservice1.suning.cn/uimg1/b2c/image/jQx5mrAnkji7zhQ80lkjqQ.jpg_800w_800h_4e", "primary":0, "pictureLocation":1 } ] } ] } } } }
String Image="上面要解析的json"; JSONObject jsonImage = JSON.parseObject(Image); Object ProdImage = JSONPath.eval(jsonImage, "$.sn_responseContent.sn_body.queryProdImage"); //图片 JSONArray aoyiImage = (JSONArray) JSONPath.eval(ProdImage, "$.resultInfo"); List aoyiImageList = (List) aoyiImage; for (int l = 0; l < aoyiImageList.size();l++) { Object oskus = aoyiImageList.get(l); Map Detail = (Map) oskus; Object urls = Detail.get("urls"); JSONArray path = (JSONArray) JSONPath.eval(urls, "$.path"); //path下没有对象了 可以直接遍历 path.forEach(pathlist->{ System.out.println(pathlist); }); }
2.JDK1.8中遍历List集合的几种方式
一、for循坏
public class ForList { public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); for (int i = 0, length = list.size(); i < length; i++) { System.out.println(list.get(i)); } } }
二、forEach循坏
与for循坏相比,forEach循坏更加简洁明了。
public class ForEachList { public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); for (Integer it : list) { System.out.println(it); } } }
三、迭代器
1、Iterator
public class IteratorTest { public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); Iterator<Integer> it = list.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } }
2、ListIterator
Iterator的子类,只能用于List集合。
public class ListIteratorTest { public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); ListIterator<Integer> it = list.listIterator(); while(it.hasNext()) { System.out.println(it.next()); } } }
四、Lambda表达式
public class LambdaTest { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.forEach(item -> { System.out.println(item); }); } }
List<Map<String, Object>> datas = iStatisticsService.statisticsList(); datas.forEach(date->{ date.forEach((k,v)-> System.out.println(k+"\t"+v)); });