[英]Convert Java Object to Json and Vice versa?
I know that JSON object is nothing but the String
. 我知道JSON对象只不过是
String
。
My question is that I have a Map of Object and i want to convert it into Json format. 我的问题是我有一个Object of Object,我想把它转换成Json格式。
Example : 示例:
Java Class ->
Class Person{
private String name;
private String password;
private int number;
}
Java list ->
Map<List<Long>,List<Person>> map=new HashMap<List<Long>,List<Person>>();
..and map has Some data filled in it.
I want to convert that list into 我想将该列表转换为
Json Format?
How I can achieve it? 我怎么能实现它? Because i want to send it over HttpClient... If not what is the other alternative way?
因为我想通过HttpClient发送它...如果不是另一种替代方式呢?
As per my knowledge there is Gson API
available, but I dont know how to use it and or in other efficient way. 据我所知,有
Gson API
可用,但我不知道如何使用它或以其他有效的方式。
Thank you 谢谢
Not sure what the problem with Gson is. 不确定Gson的问题是什么。 From the doc :
来自doc :
BagOfPrimitives obj = new BagOfPrimitives();
Gson gson = new Gson();
String json = gson.toJson(obj);
and 和
BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);
That object is (as the name suggests) made up of primitives. 该对象(顾名思义)由原语组成。 However Gson will trivially handle objects, collections of objects etc. Life gets a little more complex when using generics etc., but for your example above I would expect Gson to work with little trouble.
然而,Gson将轻松处理对象,对象集合等。使用泛型等时,生活会变得复杂一些,但对于上面的例子,我希望Gson能够轻松地处理。
Using Gson to convert to Json using Gson at client side. 使用Gson在客户端使用Gson转换为Json。
Sending String array. 发送字符串数组。
String[] subscriberArray = new String[]{"eee", "bbb"};
Gson gson = new Gson();
String recipientInfoStringFormat = gson.toJson(subscriberArray);
Sending Array of User Defined Type. 发送用户定义类型的数组。
RecipientInfo[] recipientInfos = new RecipientInfo[1];
RecipientInfo ri = new RecipientInfo();
ri.setA(1);
ri.setB("ss");
recipientInfos.add(ri);
Gson gson = new Gson();
String recipientInfoStringFormat = gson.toJson(recipientInfos);
Using Gson at Server Side to read Data. 在服务器端使用Gson读取数据。
For Primitive Types. 对于原始类型。
String subscriberArrayParam = req.getParameter("subscriberArrayParam");
Gson gson = new Gson();
String[] subscriberArray = gson.fromJson(subscriberArrayParam, String[].class);
for (String str : subscriberArray) {
System.out.println("qq :"+str);
}
For User Defined Object 对于用户定义的对象
String recipientInfos = req.getParameter("recipientInfoStringFormat");
Gson gson = new Gson();
RecipientInfo[] ri = gson.fromJson(recipientInfos, RecipientInfo[].class);
You can use jackson also. 你也可以使用杰克逊。
Person person= new Person();
ObjectMapper mapper = new ObjectMapper();
try {
// convert personobject to json string, and save to a file
mapper.writeValue(new File("c:\\person.json"), person);
// display to console
System.out.println(mapper.writeValueAsString(person));
} catch (Exception e) {
e.printStackTrace();
}
and vice versa 反之亦然
ObjectMapper mapper = new ObjectMapper();
try {
// read from file, convert it to user class
Person person= mapper.readValue(new File("c:\\person.json"), Person.class);
// display to console
System.out.println(person);
} catch (Exception e) {
e.printStackTrace();
}
for using jackson add this dependency to your POM.xml 使用jackson将此依赖项添加到您的POM.xml
<repositories>
<repository>
<id>codehaus</id>
<url>http://repository.codehaus.org/org/codehaus</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.8.5</version>
</dependency>
</dependencies>
I got my Answer but Thank you for Your Responses. 我得到了答案,但感谢您的回复。
Map<Long,List<Person>> map=new HashMap<Long,List<Person>>();
//adding some data
Gson gson=new Gson();
String mapJsonStr=gson.toJson(map);
//mapJsonStr : is my map's JSon Strin
for reverse 反向
TypeToken<Map<Long,List<Person>>> token = new TypeToken<Map<Long,List<Person>>>(){};
Map<Long,List<Person>> map_new=new HashMap<Long,List<Person>>();
map_new=gson.fromJson(mapJsonStr,token.getType());
//origian map
// map_new is my Map get from map's Json String
That's It.Thank you 就是它。谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.