简体   繁体   English

将json映射到Java对象

[英]mapping json to Java object

I send my data from client like this 我这样从客户端发送数据

$.ajax
  type: "POST"
  url: "/plan"
  dataType: "json"
  ContentType: "application/json; charset=utf-8"
  data: plan
  success:(data) ->
    alert "Save Plan Succeesfully"

In java code, I have class Plan which has same fields with data in json. 在Java代码中,我有Plan类,其中json中的数据具有相同的字段。 So now, I want to mapping from data(json) to may Plan object in Java code. 所以现在,我想在Java代码中从data(json)映射到May Plan对象。 Help me! 帮我!

尝试GSON ,一个Java的Google JSON库。

You can use jackson for that. 您可以使用杰克逊。 Read the documentation here: 在此处阅读文档:

http://wiki.fasterxml.com/JacksonHome http://wiki.fasterxml.com/JacksonHome

I would just say use a JSON parser like GSON or Jackson, the thing is by doing that you'll have to receive the entire String and parse it by hand each time (pain!). 我只是说要使用GSON或Jackson之类的JSON解析器,这样做是必须每次接收整个String并手工解析(痛苦!)。

If it's possible, I'd definitely look at using SpringMVC as by including Jackson in your servlet-context.xml you can actually pass the entire object through without doing any parsing by hand. 如果可能的话,我肯定会使用SpringMVC,因为在您的servlet-context.xml中包含了Jackson,您实际上可以传递整个对象而无需手动进行任何解析。

So you could declare a dumb object of Plan in Java and it will parse without you having to actually write any code intermediately to interpret the JSON string into an object. 因此,您可以在Java中声明一个Plan的哑对象,并且无需进行中间编写任何代码即可将JSON字符串解释为对象的情况下进行解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM