分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

json笔记

发布时间:2023-09-06 01:30责任编辑:林大明关键词:jsjson
<!DOCTYPE html><html><head><meta charset="UTF-8"><meta http-equiv="pragma" content="no-cache" /><meta http-equiv="cache-control" content="no-cache" /><meta http-equiv="Expires" content="0" /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><script type="text/javascript">var json = {"key":123,"key2":"hello","key3":[1,"nihao",true],"key4":{"key4_1":99,"key4_2":"dani"},"key5":[{"key5_1":199,"key5_2":"dani"},{"key5_3":199,"key5_4":"123dani"}]};/* alert(json.key5[1].key5_4); */var ?str= JSON.stringify(json);//json转成对象//alert(str);var jsonObj = JSON.parse(str);/* alert( jsonObj ); */alert( jsonObj.key3[2] );</script></head><body></body></html>

  

json对象由在括号括起来,对象中的属性也就是json的key是一个字符串,所以一定要使用双引号引起来。每组key之间使用逗号进行分隔。可以保持的格式很多,客户端和服务器都容易解读

var jsons = {"key1":"abc", // 字符串类型"key2":1234, ?// Number"key3":[1234,"21341","53"], // 数组"key4":{ ???????????????????// json类型"key4_1" : 12,"key4_2" : "kkk"},"key5":[{ ?????????????????// json数组 ???"key5_1_1" : 12, ???"key5_1_2" : "abc"},{ ???"key5_2_1" : 41, ???"key5_2_2" : "bbj"}]};

JSON中两个常用的方法。
JSON对象和字符串对象的互转。 注意:JSON大写
JSON.stringify( json ); ??????此方法可以把一个json对象转换成为json字符串 ??
JSON.parse( jsonString ); ?????此方法可以把一个json字符串转换成为json对象

json在java中的操作。常见的有三种情况。
1.java对象和json的转换
2.java对象list集合和json的转换
3.map对象和json的转换

引入谷歌Gson 的jar包

package com.atguigu.bean;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.junit.Test;import com.google.gson.Gson;import com.google.gson.JsonElement;import com.google.gson.reflect.TypeToken;public class GsonTest { ???@Test ???public void getGson(){ ???????????????Person person = new Person(1,"张三"); ???????????????Gson gson = new Gson(); ???????????????//对象转json格式 ???????String json = gson.toJson(person);// ???????System.out.println(json);// ???????System.out.println("=============="); ???????????????//json格式转单个对象 ???????Person fromJson = gson.fromJson(json, Person.class);// ???????System.out.println(fromJson); ???????????????//一群对象呢? ???????List<Person> list = new ArrayList<Person>(); ???????list.add(new Person(2,"张三1")); ???????????????list.add(new Person(3,"张三3")); ???????????????list.add(new Person(4,"张三4")); ???????????????????????JsonElement jsonTree = gson.toJsonTree(list);// ???????System.out.println(jsonTree); ???????????????List<Person> fromJson2 = gson.fromJson(jsonTree, new TypeToken<List<Person>>(){}.getType());// ???????System.out.println(fromJson2); ???????????} ???@Test ???public void mapJson(){ ???????Map<String,Person> map = new HashMap<String,Person>(); ???????????map.put("1", new Person(1,"haha")); ???????????map.put("2", new Person(2,"2haha")); ???????????map.put("3", new Person(3,"3haha")); ???????????????????????Gson gson = new Gson(); ???????????JsonElement map1 = gson.toJsonTree(map);//map对象保存为json格式 ???????????System.out.println(map1); ???????????//把json格式转换为map对象 ???????????Map<String,Person> fromJson = gson.fromJson(map1, new TypeToken<HashMap<String,Person>>(){}.getType()); ???????????System.out.println(fromJson); ???????????????} ???}
View Code

 

 

 

 

 

json笔记

原文地址:http://www.cnblogs.com/JavaBlackHole/p/8035343.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved