简体   繁体   中英

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. So now, I want to mapping from data(json) to may Plan object in Java code. Help me!

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

You can use jackson for that. Read the documentation here:

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!).

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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