gson对json与对象与list集合的转换

import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.reflect.TypeToken;

public class TestJson {
	public static void main(String[] args) {

		// 某对象转换成json
		// test();
		// json 转换成对象
		// test1();
		// 将集合转换成json格式字符串
		test2();
	}

	private static void test2() {
		// 初始化数据对象
		ArrayList<Person> arrayList = new ArrayList<Person>();
		JsonArray jsonArray = new JsonArray();
		Person person = null;

		// 初始化arraylist容器 100个person对象
		for (int i = 0; i < 100; i++) {
			person = new Person();
			person.setAge(i + "age");
			person.setName(i + "name");
			arrayList.add(person);
		}

		Gson gson = new Gson();
		// 将list集合变成json格式
		String str = gson.toJson(arrayList);
		System.out.println(str);
		// 将str json格式变成 list格式
		List<Person> list = gson.fromJson(str, new TypeToken<List<Person>>() {
		}.getType());
		// 输出list集合
		for (int i = 0; i < 10; i++) {
			System.out.println(list.get(i).getName());
		}
	}

	private static void test() {
		// 封装 person对象
		Person person = new Person();
		person.setAge("age");
		person.setName("name");
		person.setSex("sex");
		// 创建gson对象
		Gson gson = new Gson();
		// 使用gson tojson方法将 person对象转换成json
		String str = gson.toJson(person);
		// 输出转换后的格式
		System.out.println(str);

	}

	private static void test1() {
		// json格式数据
		String str = "{\"name\":\"name\",\"sex\":\"sex\",\"age\":\"age\"}";
		// 输出信息
		System.out.println(str);
		// 创建gson对象
		Gson gson = new Gson();
		// 使用gson对象 fromJson方法 将 json转换成person对象
		Person person = gson.fromJson(str, Person.class);
		// 输出对象的属性
		System.out.println("person age: " + person.getAge());
	}
}

输出结果

[{"name":"0name","age":"0age"},{"name":"1name","age":"1age"},{"name":"2name","age":"2age"},{"name":"3name","age":"3age"},{"name":"4name","age":"4age"},{"name":"5name","age":"5age"},{"name":"6name","age":"6age"},{"name":"7name","age":"7age"},{"name":"8name","age":"8age"},{"name":"9name","age":"9age"},{"name":"10name","age":"10age"},{"name":"11name","age":"11age"},{"name":"12name","age":"12age"},{"name":"13name","age":"13age"},{"name":"14name","age":"14age"},{"name":"15name","age":"15age"},{"name":"16name","age":"16age"},{"name":"17name","age":"17age"},{"name":"18name","age":"18age"},{"name":"19name","age":"19age"},{"name":"20name","age":"20age"},{"name":"21name","age":"21age"},{"name":"22name","age":"22age"},{"name":"23name","age":"23age"},{"name":"24name","age":"24age"},{"name":"25name","age":"25age"},{"name":"26name","age":"26age"},{"name":"27name","age":"27age"},{"name":"28name","age":"28age"},{"name":"29name","age":"29age"},{"name":"30name","age":"30age"},{"name":"31name","age":"31age"},{"name":"32name","age":"32age"},{"name":"33name","age":"33age"},{"name":"34name","age":"34age"},{"name":"35name","age":"35age"},{"name":"36name","age":"36age"},{"name":"37name","age":"37age"},{"name":"38name","age":"38age"},{"name":"39name","age":"39age"},{"name":"40name","age":"40age"},{"name":"41name","age":"41age"},{"name":"42name","age":"42age"},{"name":"43name","age":"43age"},{"name":"44name","age":"44age"},{"name":"45name","age":"45age"},{"name":"46name","age":"46age"},{"name":"47name","age":"47age"},{"name":"48name","age":"48age"},{"name":"49name","age":"49age"},{"name":"50name","age":"50age"},{"name":"51name","age":"51age"},{"name":"52name","age":"52age"},{"name":"53name","age":"53age"},{"name":"54name","age":"54age"},{"name":"55name","age":"55age"},{"name":"56name","age":"56age"},{"name":"57name","age":"57age"},{"name":"58name","age":"58age"},{"name":"59name","age":"59age"},{"name":"60name","age":"60age"},{"name":"61name","age":"61age"},{"name":"62name","age":"62age"},{"name":"63name","age":"63age"},{"name":"64name","age":"64age"},{"name":"65name","age":"65age"},{"name":"66name","age":"66age"},{"name":"67name","age":"67age"},{"name":"68name","age":"68age"},{"name":"69name","age":"69age"},{"name":"70name","age":"70age"},{"name":"71name","age":"71age"},{"name":"72name","age":"72age"},{"name":"73name","age":"73age"},{"name":"74name","age":"74age"},{"name":"75name","age":"75age"},{"name":"76name","age":"76age"},{"name":"77name","age":"77age"},{"name":"78name","age":"78age"},{"name":"79name","age":"79age"},{"name":"80name","age":"80age"},{"name":"81name","age":"81age"},{"name":"82name","age":"82age"},{"name":"83name","age":"83age"},{"name":"84name","age":"84age"},{"name":"85name","age":"85age"},{"name":"86name","age":"86age"},{"name":"87name","age":"87age"},{"name":"88name","age":"88age"},{"name":"89name","age":"89age"},{"name":"90name","age":"90age"},{"name":"91name","age":"91age"},{"name":"92name","age":"92age"},{"name":"93name","age":"93age"},{"name":"94name","age":"94age"},{"name":"95name","age":"95age"},{"name":"96name","age":"96age"},{"name":"97name","age":"97age"},{"name":"98name","age":"98age"},{"name":"99name","age":"99age"}]

0name

1name

2name

3name

4name

5name

6name

7name

8name

9name

时间: 2024-11-09 10:42:50

gson对json与对象与list集合的转换的相关文章

Gson解析json字符串、json数组转换成对象

实体类: public class Product { private int id; private String name; private String date; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name

Json转java对象和List集合

public static void main(String[] args) { // 转换对象 String strJson ="{\"basemenu_id\":\"1\",\"basemenu_name\":\"aaaaaa\",\"basemenu_order\":\"2\"}"; aaaa jb = (aaaa)json2Object(strJson,aaa

&lt;摘录&gt;Gson对Java嵌套对象和JSON字符串之间的转换

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,具有良好的跨平台特性.近几年来已经和XML一样成为C/S架构中广泛采用的数据格式.有关JSON的更多知识,请参考以下内容:http://json.org/json-zh.html 在服务器和客户端之间使用JSON数据格式进行通信,经常会涉及到JAVA对象和JSON字符串之间的转换.通常,我们可以使用一些JSON解析工具,例如:Gson,FastJson等.当然,我们也可以手动解析,只是会比较繁琐. 下面

jackson json和对象,集合的几种转换

jackson  json和对象,集合的几种转换 package com.crm.net.util; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.List; import org.codehaus.jackson.JsonParseException

转载-------- JSON 与 对象 、集合 之间的转换 JSON字符串和java对象的互转【json-lib】

转载--*--*---- 在开发过程中,经常需要和别的系统交换数据,数据交换的格式有XML.JSON等,JSON作为一个轻量级的数据格式比xml效率要高,XML需要很多的标签,这无疑占据了网络流量,JSON在这方面则做的很好,下面先看下JSON的格式, JSON可以有两种格式,一种是对象格式的,另一种是数组对象, {"name":"JSON","address":"北京市西城区","age":25}//JS

android之GSON解析JSON

Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库. 比如: <pre name="code" class="java">public class Order { public String id; public String OrderName; @Override public String toString() { return "id:"+id+",OrderNa

Android中使用Gson解析JSON数据的两种方法

Json是一种类似于XML的通用数据交换格式,具有比XML更高的传输效率;本文将介绍两种方法解析JSON数据,需要的朋友可以参考下 Json是一种类似于XML的通用数据交换格式,具有比XML更高的传输效率. 从结构上看,所有的数据(data)最终都可以分解成三种类型: 第一种类型是标量(scalar),也就是一个单独的字符串(string)或数字(numbers),比如"北京"这个单独的词. 第二种类型是序列(sequence),也就是若干个相关的数据按照一定顺序并列在一起,又叫做数组

Android网络之数据解析----使用Google Gson解析Json数据

[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4063452.html 联系方式:[email protected] [正文] 文章回顾: Android网络之数据解析----SAX方式解析XML数据 一.Json数据的介绍                                                             

记录--Gson、json转实体类、类转json

需要导入Gson jar包 最近在做一个java web service项目,需要用到jason,本人对java不是特别精通,于是开始搜索一些java平台的json类库. 发现了google的gson,因为之前对于protocolbuf有一些了解,带着一些好奇心,我开始使用了gson. 经过比较,gson和其他现有java json类库最大的不同时gson需要序列化得实体类不需要使用annotation来标识需要序列化得字段,同时gson又可以通过使用annotation来灵活配置需要序列化的字