2016-07-12 31 views
0

我正在给JAVA API的调用如下:如何检索JSON从AJAX调用传递到Java API

var userDetails = { 
      userId: userId, 
      first : "1 one", 
      second : "2 two" 
     } 
    $.ajax({ 
     type : 'POST', 
     url : "http://" + config.domain + config.root + "/myExp/allExperiment", 
     dataType : "json", 
     data : userDetails, 
     success : function(data) {}) 
    }); 

并试图获得传递的对象如下:

@RequestMapping(value = "/allExperiment", method = RequestMethod.POST) 
    public JsonMapModel getAllDatasets(@RequestBody String userDetails) { 
     System.out.println("Data is " + userDetails); 
} 

我在API 数据正在逐渐以下是秒= 2 + 2 &用户id = 16 &第一= 1 +一个

任何想法如何将上述响应转换为JSONObject或任何其他集合,以便我可以适当地引用传递的JSON。

+0

您在Java端使用什么库来处理请求? – smac89

+0

目前没有使用过的库,它只是一个基于Spring的API,喜欢知道我应该怎样使用/做解析请求体 – Nitin

回答

0

感谢您的答复,按照由@Alexandru码头的建议,以下为我工作再次

0

它应该工作,如果你用一个真实的对象,例如UserDetails userDetails替换String userDetails)。

或尝试使用MultiValueMap而不是String。从下面的链接看来,如果这是参数比Spring自动使用FormHttpMessageConverter。 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestbody

+0

是的,它可以这样做,但我想有一个机制,我可以维护一个服务器端对象,而不考虑JSON中的参数数量,例如,如果传递JSON有2个键值对,或者它可以是5个键值对,应该将其存储在服务器上的通用集合对象中 – Nitin

+1

尝试使用MultiValueMap 而不是String。从下面的链接看来,如果这是参数比Spring自动使用FormHttpMessageConverter。 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestbody –

+0

是的,它使用MultiValueMap Nitin

0

您可以使用Jersey API将JSON转换为服务器端的值对象。 Jersey Jar自动将JSON转换为VO和VO至JSON。 下面是快照如何可以在服务器端收到VO:一旦

@RequestMapping(value = "/allExperiment", method = RequestMethod.POST) 
    public JsonMapModel getAllDatasets(@RequestBody MultiValueMap<String, String> userDetails) { 
System.out.println("User id is " userDetails.getFirst("userId")); } 

感谢:

@POST 
@Path("/allExperiment") 
@Consumes({ MediaType.APPLICATION_JSON }) 
@Produces({ MediaType.APPLICATION_JSON }) 
public JsonMapModel getAllDatasets(UserDetails userDetails) { 
    System.out.println("Data is " + userDetails); 
} 
1

您使用杰克逊库。杰克逊转换Java对象到/从JSON

(pom.xml中)

<!-- Jackson --> 
<dependency> 
    <groupId>com.fasterxml.jackson.core</groupId> 
    <artifactId>jackson-core</artifactId> 
    <version>2.4.3</version> 
</dependency> 
<dependency> 
    <groupId>com.fasterxml.jackson.core</groupId> 
    <artifactId>jackson-databind</artifactId> 
    <version>2.4.3</version> 
</dependency> 

(JS)

var userDetails = { 
      userId: userId, 
      first : "1 one", 
      second : "2 two" 
     } 

    userDetails = JSON.stringify(userDetails);  

    $.ajax({ 
     type : 'POST', 
     url : "http://" + config.domain + config.root + "/myExp/allExperiment", 
     contentType : 'application/json', 
     data : userDetails, 

     success : function(data) { 

    }, 
    error : function(request, status, error) { 

    } 
}); 

(型号)

public class TestModel { 

    private String userId; 
    private String first; 
    private String second; 

    //getter, setter 
} 

(控制器)

@RequestMapping(value = "/allExperiment", method = RequestMethod.POST) 
public @ResponseBody String getAllDatasets(@RequestBody TestModel userDetails) { 


    return null; // break point, check model. 
} 
+1

而不是包括多个杰克逊图书馆,我认为'谷歌gson'就足够工作。另外我认为它应该是'数据:JSON.stringify(userDetails)'在Ajax调用。 –

+0

哦。好的。我认为你的评论很好。好。编辑。谢谢! – Byeon0gam