开发者

How can i give json input in my web application?

开发者 https://www.devze.com 2023-03-13 02:07 出处:网络
I am working with a web application which accepts json object, parses it and create a User object. I开发者_C百科 want to give this json input in gson.fromJson(jsonData, User.class);:

I am working with a web application which accepts json object, parses it and create a User object. I开发者_C百科 want to give this json input in gson.fromJson(jsonData, User.class);:

 {
   users:[{
   name: "Jack",
   email: "email1",
   friends:[{
       name: "name2",
       email: "email2",

    }]
 }]
} 

what should i do to give this json object in place of jsonData. I am working with google app engine.

Thanks in advance.


If you use Gson, please read doc here. As jsonData you can pass string with your json or Reader which streams your json data.

UPD: In your case you need to deserialize array, so use it as

String jsonData = "{"
   "users:[{ " +
   "name: 'Jack', " +
   "email: 'email1', " +
   "friends:[{ " +
   "    name: 'name2', " +
   "    email: 'email2', " +
   "  }] " +
   "}] " +
"}";
User[] users = gson.fromJson(jsonData, User[].class); 

GSon uses reflections to set properties, so make sure you have setters for all properties in json, like User.setName, User.setEmail, User.setFriends, ..

0

精彩评论

暂无评论...
验证码 换一张
取 消