首页新闻找找看学习计划

请教:Java实体类转json对象,求demo

0
[待解决问题]

有人知道Java实体类怎么转json对象吗?最好有demo,万分感谢!!!

雪剑残云的主页 雪剑残云 | 菜鸟二级 | 园豆:217
提问于:2016-10-28 10:31
< >
分享
所有回答(6)
0

1、List
Java代码
boolean[] boolArray =newboolean[]{true,false,true};
JSONArray jsonArray1 = JSONArray.fromObject( boolArray );
System.out.println( jsonArray1 );
// prints [true,false,true]

List list =newArrayList();
list.add("first");
list.add("second");
JSONArray jsonArray2 = JSONArray.fromObject( list );
System.out.println( jsonArray2 );
// prints ["first","second"]

JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']");
System.out.println( jsonArray3 );
// prints ["json","is","easy"]

2、Map
Java代码
Map map =newHashMap();
map.put("name","json");
map.put("bool", Boolean.TRUE );

map.put("int",newInteger(1) );
map.put("arr",newString[]{"a","b"} );
map.put("func","function(i){ return this.arr[i]; }");
JSONObject json = JSONObject.fromObject( map );
System.out.println( json );
//{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"name":"json","bool":true}

3、BEAN
Java代码
/**
* Bean.java
private String name = "json";
private int pojoId = 1;
private char[] ptions = new char[]{'a','f'};
private String func1 = "function(i){ return this.options[i]; }";
private JSONFunction func2 = new JSONFunction(new String[]{"i"},"return this.options[i];");
*/
JSONObject jsonObject = JSONObject.fromObject(newJsonBean() );
System.out.println( jsonObject );
//{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}

4、BEANS
Java代码
/**
* private int row ;
private int col ;
private String value ;
*
*/
List list =newArrayList();
JsonBean2 jb1 =newJsonBean2();
jb1.setCol(1);
jb1.setRow(1);
jb1.setValue("xx");

JsonBean2 jb2 =newJsonBean2();
jb2.setCol(2);
jb2.setRow(2);
jb2.setValue("");

list.add(jb1);
list.add(jb2);

JSONArray ja = JSONArray.fromObject(list);
System.out.println( ja.toString() );
//[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]

5、String to bean
Java代码
String json ="{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";
JSONObject jsonObject = JSONObject.fromString(json);
Object bean = JSONObject.toBean( jsonObject );
assertEquals( jsonObject.get("name"), PropertyUtils.getProperty( bean,"name") );
assertEquals( jsonObject.get("bool"), PropertyUtils.getProperty( bean,"bool") );
assertEquals( jsonObject.get("int"), PropertyUtils.getProperty( bean,"int") );
assertEquals( jsonObject.get("double"), PropertyUtils.getProperty( bean,"double") );
assertEquals( jsonObject.get("func"), PropertyUtils.getProperty( bean,"func") );
List expected = JSONArray.toList( jsonObject.getJSONArray("array") );
assertEquals( expected, (List) PropertyUtils.getProperty( bean,"array") );

Java代码
String json ="{\"value\":\"xx\",\"row\":1,\"col\":1}";
JSONObject jsonObject = JSONObject.fromString(json);
JsonBean2 bean = (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.class);
assertEquals( jsonObject.get("col"),newInteger( bean.getCol()) );
assertEquals( jsonObject.get("row"),newInteger( bean.getRow() ) );
assertEquals( jsonObject.get("value"), bean.getValue() );

6 json to xml
1)
JSONObject json = new JSONObject( true );
String xml = XMLSerializer.write( json );

<o class="object" null="true">

2)
JSONObject json = JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}");
String xml = XMLSerializer.write( json );
<o class="object">
<name type="string">json</name>
<bool type="boolean">true</bool>
<int type="number">1</int>
</o>
<o class="object">
<name type="string">json</name>
<bool type="boolean">true</bool>
<int type="number">1</int>
</o>
3)
JSONArray json = JSONArray.fromObject("[1,2,3]");
String xml = XMLSerializer.write( json );
<a class="array">
<e type="number">1</e>
<e type="number">2</e>
<e type="number">3</e>
</a>

7 、xml to json
<a class="array">
<e type="function" params="i,j">
return matrix[i][j];
</e>
</a>
<a class="array">
<e type="function" params="i,j">
return matrix[i][j];
</e>
</a>

JSONArray json = (JSONArray) XMLSerializer.read( xml );
System.out.println( json );
// prints [function(i,j){ return matrix[i][j]; }]

复返 | 园豆:215 (菜鸟二级) | 2016-10-28 10:36
0

序列化下不就行了吗?很难吗?

 

~扎克伯格 | 园豆:1799 (小虾三级) | 2016-10-28 10:55

您好,我是新手,不太会啊!!!

支持(0) 反对(0) 雪剑残云 | 园豆:217 (菜鸟二级) | 2016-10-28 10:57

@雪剑残云: 不知道可以百度的,Java如何将实体对象转换为Json

支持(0) 反对(0) ~扎克伯格 | 园豆:1799 (小虾三级) | 2016-10-28 10:59
0

最简单的方法,用阿里的fastjson,一键转换。

ㄓㄤㄑㄧㄤ | 园豆:251 (菜鸟二级) | 2016-11-01 17:12
0

使用jackson包(com.fasterxml.jackson),请百度

ObjectMapper mapper = new ObjectMapper();
Object obj = mapper.readValue(jsonString, clazz);

// jsonString 是JSON字符串
// clazz 是简单POJO对象类型

 

安静的下雪天 | 园豆:274 (菜鸟二级) | 2016-11-29 10:52
0
 1 package com.gson.test;
 2 
 3 import java.util.Arrays;
 4 
 5 import com.google.gson.Gson;
 6 import com.google.gson.GsonBuilder;
 7 
 8 public class GsonTest {
 9 
10     public static void main(String[] args) {
11     
12             String jsonStr="{\"title\": \"Java Puzzlers: Traps, Pitfalls, and Corner Cases\",  \"isbn10\": \"032133678X\",  \"isbn13\": \"978-0321336781\", \"authors\": [ \"Joshua Bloch\", \"Neal Gafter\" ]}";
13             Gson gson = new GsonBuilder().create();
14             Book p = gson.fromJson(jsonStr, Book.class);
15             System.out.println(p);
16         
17     }
18 
19     class Book {
20 
21         private String[] authors;
22         private String isbn10;
23         private String isbn13;
24         private String title;
25 
26         @Override
27         public String toString() {
28             return "Book [authors=" + Arrays.toString(authors) + ", isbn10="
29                     + isbn10 + ", isbn13=" + isbn13 + ", title=" + title + "]";
30         }
31 
32     }
33 }

我这边用的是谷歌的开源jar包gson-2.2.4.jar

梦蓝蓝的 | 园豆:208 (菜鸟二级) | 2016-11-30 11:38
0

FastJSON 阿里封装的,号称是最快的,down一个jar的功夫

既然琴瑟起 | 园豆:210 (菜鸟二级) | 2017-01-06 17:12
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册