分享web开发知识

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

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

浅谈json

发布时间:2023-09-06 01:14责任编辑:顾先生关键词:jsjson

1.什么是json

  json:就是一种轻量级的数据交换格式

     json语法:“键”:“值”组成,多个之间通过逗号隔开,{}表示对象,[]表示数组

  json字符串: ‘{"a": "Hello", "b": "World"}‘;

  json对象: {a: ‘Hello‘, b: ‘World‘};

  json数组: ["a":"Hello"] ;

  json对象数组: [ {"name":"张三","age":20},

           {"name":"李四","age":21} ]

2.前台

  将对象转为json:JSON.stringify(对象)

  将json转为对象:JSON.parse(json)

3.后台(总的来说就是将对象、集合转为json,将字符串json转为对象、集合)

  一:Google的Gson,需要json.jar包,(fromJson()将json字符串转为对象、集合,toJson()将集合、对象转为json)

    1、将json字符串转为对象:gson对象.fromJson(实体, 实体.class) ;

package com.cn.test.test;import com.cn.test.pojo.TestStudent;import com.google.gson.Gson;public class Test { ???????public static void main(String[] args) { ???????String student = "{‘name‘:‘张三‘,‘age‘:20}" ; ???????Gson gson = new Gson() ; ???????TestStudent s = gson.fromJson(student, TestStudent.class) ; ???????System.out.println("-----------name----------"+s.getName()); ???????System.out.println("-----------age-----------"+s.getAge()); ???}}

    2、将json字符串转为集合: gson对象.fromJson(实体, new TypeToken<List<实体>>(){}.getType()) ; TypeToken是gson提供的数据类型转换器,可以支持各种数据集合类型转换。

package com.cn.test.test;import java.util.List;import com.cn.test.pojo.TestStudent;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;public class Test { ???????public static void main(String[] args) { ???????String student = "[{‘name‘:‘张三‘,‘age‘:20},{‘name‘:‘李四‘,‘age‘:21}]" ; ???????Gson gson = new Gson() ; ???????List<TestStudent> s = gson.fromJson(student, new TypeToken<List<TestStudent>>(){}.getType()) ; ???????for (TestStudent testStudent : s) { ???????????System.out.println("-----------name----------"+testStudent.getName()); ???????????System.out.println("-----------age-----------"+testStudent.getAge()); ???????} ???}}

    3、将对象转为json:toJson(对象)

package com.cn.test.test;import com.cn.test.pojo.TestStudent;import com.google.gson.Gson;public class Test { ???????public static void main(String[] args) { ???????TestStudent s = new TestStudent() ; ???????s.setAge(20); ???????s.setName("张三"); ???????Gson gson = new Gson() ; ???????String student = gson.toJson(s) ; ???????System.out.println("------------------"+student); ???}}
结果:------------------{"name":"张三","age":20}

    4、将集合转为json:toJson(集合)

package com.cn.test.test;import java.util.ArrayList;import java.util.List;import com.cn.test.pojo.TestStudent;import com.google.gson.Gson;public class Test { ???????public static void main(String[] args) { ???????List<TestStudent> student = new ArrayList<TestStudent>() ; ???????TestStudent s = new TestStudent() ; ???????s.setAge(20); ???????s.setName("张三"); ???????TestStudent s1 = new TestStudent() ; ???????s1.setAge(30); ???????s1.setName("李四"); ???????student.add(s) ; ???????student.add(s1) ; ???????Gson gson = new Gson() ; ???????String testStudent = gson.toJson(student) ; ???????System.out.println("------------------"+testStudent); ???}}
结果:------------------[{"name":"张三","age":20},{"name":"李四","age":30}]

   二:

 

浅谈json

原文地址:http://www.cnblogs.com/-scl/p/7599481.html

知识推荐

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