嗨,任何人都可以告诉我如何在休息消费方法中处理这个json
在我的客户端,我有这个代码:
var search_form_data_array = new Object();
search_form_data_array[0] = JSON.stringify({name: "dept",
type:"equal",
value: "233"});
search_form_data_array[1] = JSON.stringify({ name: "deptDesc",
type:"equal",
value:"depts" });
$.ajax({
url: globalvars.otherDiscoveredSearchFormUri,
type:'POST',
data:{"SearchFormInput":search_form_data_array},
contentType:'application/json',
success: function(){}
});
我的服务器端代码:
@POST
@Path("/SearchForm")
@Consumes(MediaType.APPLICATION_JSON)
public String getOtherChargesSerachData(SearchFormInput data)
return data.name;
}
public static class SearchFormInput {
public String name;
public String type;
public String value;
}
答案 0 :(得分:0)
您可以将XStream用作以下内容阅读:
protected Object loadFromJSONRequest(String strJSON, Map<String, Class> aliasMap) {
String aliasValue = "";
try {
XStream xStream = new XStream(new JettisonMappedXmlDriver());
aliasValue = includeAliasesForRequest(xStream, aliasMap);
return xStream.fromXML(strJSON);
} catch (XStreamException e) {
logExceptions(BaseResource.class, e.getStackTrace().toString());
throw new PubGUIErrorResponse("error." + aliasValue + ".conversion");
} catch (Exception e) {
logExceptions(BaseResource.class, e.getMessage().toString());
throw new PubGUIErrorResponse("error.conversion");
}
}
答案 1 :(得分:0)
您可以使用Google的Gson将json序列化/反序列化为Java对象。
要解析数组,您可以使用类似的东西。请注意,该参数不能是SearchFormInput,因为当您发送一个带有列表的虚拟json对象时。
@POST
@Path("/SearchForm")
@Consumes(MediaType.APPLICATION_JSON)
public String getOtherChargesSerachData(String data){
// You will need to parse data if you want to send something more than the array
Gson gson = new Gson();
Type listType = new TypeToken<List<SearchFormInput>>() {}.getType();
List<SearchFormInput> searchFormInputList = gson.fromJson(data, listType);
// iterate searchFormInputList to do whatever you want
return "Whatever string you want to return";
}
如果您这样发送,
$.ajax({
url: globalvars.otherDiscoveredSearchFormUri,
type:'POST',
data:{"SearchFormInput":search_form_data_array},
contentType:'application/json',
success: function(){}
});
您将需要从值“SearchFormInput”获取数组,然后解析它。 如果您不需要这个,您只能发送数组。类似的东西:
var stringArray = JSON.stringify(search_form_data_array);
...,
data: stringArray,
...,