json笔记

<!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

 

 

 

 

 

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