本文介绍了如何利用泽西休息Web服务和Java解析JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到来自iOS的客户端JSON数组,并希望使用Java和新泽西州和GSON解析JSON在服务器端。我送来自iOS的POST方法的JSON阵列。我想消耗JSON,但停留在如何保存在Java类JSON数据。这是我的JSON数组结构
{
好友列表: [
{ID:1,用户名:USER1,名:PERSON1,friendUsername:fUser1,FRIENDNAME:fName1},
{ID:2,用户名:用户2,名:PERSON2,friendUsername:fUser2,FRIENDNAME:fName2},
{ID:3,用户名:用户3,名:Person3可能,friendUsername:fUser3,FRIENDNAME:fName3},...
]
}
下面是我的Web服务类
@Path(/ FriendsList)
公共类RestWebServicesAPI {
@POST
@Path(/朋友)
@Consumes(MediaType.APPLICATION_JSON)
市民朋友saveFriedList(朋友的朋友,@Context HttpServletRequest的请求){ //不知道如何解析JSON数组???? }
}
和这里是我的朋友班
进口的java.util.List;进口javax.xml.bind.annotation.XmlRootElement;@XmlRootElement公共类友{ 私人字符串ID;
私人字符串用户名;
私人字符串名称;
私人字符串friendUsername;
私人字符串FRIENDNAME; 众友(){
} //的getter setter方法}
解决方案
我认为你必须做的只是这样的:
@Path(/ FriendsList)
公共类RestWebServicesAPI {@POST
@Path(/朋友)
@Consumes(MediaType.APPLICATION_JSON)
市民朋友saveFriendList(最后弦乐JSON){
GSON GS =新GSON();
朋友[] N = gs.fromJson(JSON,友[]的.class);}
//替代
@POST
@Path(/朋友)
@Consumes(MediaType.APPLICATION_JSON)
市民朋友saveFriendList(最终好友[]朋友){
}
I am getting Json array from iOS client and want to parse the Json in server side using Java and jersey and Gson. I am sending the JSON array in POST method from iOS. I want to consume the json but stuck on how do i save the json data in Java class. This is the structure of my Json array
{
"friendList": [
{"id": 1, "username": "user1", "name":"person1", "friendUsername":"fUser1", "friendName":"fName1"},
{"id": 2, "username": "user2", "name":"person2", "friendUsername":"fUser2", "friendName":"fName2"},
{"id": 3, "username": "user3", "name":"person3", "friendUsername":"fUser3", "friendName":"fName3"},...
]
}
Here is my web services Class
@Path("/FriendsList")
public class RestWebServicesAPI {
@POST
@Path("/friends")
@Consumes(MediaType.APPLICATION_JSON)
public Friends saveFriedList(Friends friend, @Context HttpServletRequest request) {
// Don't know how to parse json array????
}
}
and Here is my Friends Class
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
Public Class Friends {
private String id;
private String username;
private String name;
private String friendUsername;
private String friendName;
public Friends() {
}
//getter setter methods
}
解决方案
I think you have to do simply like this:
@Path("/FriendsList")
public class RestWebServicesAPI{
@POST
@Path("/friends")
@Consumes(MediaType.APPLICATION_JSON)
public Friends saveFriendList(final String json){
Gson gs = new Gson();
Friends [] n = gs.fromJson(json, Friends [].class);
}
//ALTERNATIVE
@POST
@Path("/friends")
@Consumes(MediaType.APPLICATION_JSON)
public Friends saveFriendList(final Friends[] friends){
}
这篇关于如何利用泽西休息Web服务和Java解析JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!