Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Jersey Rest Webservices和Java解析JSON数组_Java_Json_Web Services_Jersey_Arrays - Fatal编程技术网

如何使用Jersey Rest Webservices和Java解析JSON数组

如何使用Jersey Rest Webservices和Java解析JSON数组,java,json,web-services,jersey,arrays,Java,Json,Web Services,Jersey,Arrays,我从iOS客户端获取Json数组,并希望使用Java、jersey和Gson在服务器端解析Json。我正在从iOS发送POST方法中的JSON数组。我想使用json,但仍停留在如何在Java类中保存json数据的问题上。这是我的Json数组的结构 { "friendList": [ {"id": 1, "username": "user1", "name":"person1", "friendUsername":"fUser1", "friendName":"fName1"}

我从iOS客户端获取Json数组,并希望使用Java、jersey和Gson在服务器端解析Json。我正在从iOS发送POST方法中的JSON数组。我想使用json,但仍停留在如何在Java类中保存json数据的问题上。这是我的Json数组的结构

{
    "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"},...
    ]
}
这是我的web服务课程

@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????

     }


}
这是我的朋友班

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

} 

我认为你必须这样做:

@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){


    }

请遵循以下来源

@POST
@Path("/friends")
@Consumes(MediaType.APPLICATION_JSON)
public Friends saveFriendList(Friends friends){
 // saveFriend function should have business logic to store your data in db
 friends= saveFriend(friends);
}

如何将每个json值映射到friends类中的变量?如果您想在rest服务中解析json,我编辑的答案可能会起作用。在我的代码的第一种情况下,会得到一个json,它会在friends数组中用GSON解析(GSON会自动解析);在第二部分中,自动解析Friendsis my Friends类代码数组中接收到的Json是正确的还是需要将其与Json匹配?Json中的变量名和参数名必须相同,这是唯一的条件谢谢..我不理解saveFriend(Friends),你的意思是我需要在Friends类中创建saveFriend方法,它接受friend对象并将数据保存在数据库中?你能帮我写一些朋友课的代码吗。