weixin_33709590
weixin_33709590
2012-12-29 10:20
采纳率: 0%
浏览 113

Jersey,使用JSON数组

Hi Can any one tell me how can i handle this json in rest consuming method

On my client Side i have this code :

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(){}
});

My server side code:

@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; 
}

图片转代码服务由CSDN问答提供 功能建议

                    

嗨,任何人都可以告诉我如何使用耗费大量精力的方法处理此json

在我的客户端上,我有以下代码:

  var search_form_data_array = new Object();

 search_form_data_array [0] = JSON.stringify({name:“ dept”,
     类型:“等于”,
     值:“ 233”});

 search_form_data_array [1] = JSON.stringify({name:“ deptDesc”,
     类型:“等于”,
     value:“ depts”});

 $ .ajax({
     网址:globalvars.otherDiscoveredSearchFormUri,
     输入:“ POST”,
     数据:{“ SearchFormInput”:search_form_data_array},
     contentType:“ application / json”,
     成功:function(){}
 });
  

 

我的服务器端代码:

  @POST
 @Path(“ / SearchForm”)
 @Consumes(MediaType.APPLICATION_JSON)
 公共字符串getOtherChargesSerachData(SearchFormInput数据)

     返回data.name;
 }

 公共静态类SearchFormInput {
     公共字符串名称;
     公共字符串类型;
     公共字符串值;
 }
  
     
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • csdnceshi68
    local-host 2012-12-29 10:40

    You can use XStream for reading as:

    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");
        }
    }
    
    点赞 评论
  • weixin_33738555
    weixin_33738555 2012-12-29 12:15

    You can use Gson from Google to serialize/deserialize json to/from Java objects.

    To parse the array you can use something like this. Note that the parameter cannot be SearchFormInput, because as you are sending a dummy json object with a list inside it.

    @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";
    }
    

    If you send it like this,

    $.ajax({
        url: globalvars.otherDiscoveredSearchFormUri,
        type:'POST',
        data:{"SearchFormInput":search_form_data_array},
        contentType:'application/json',
        success: function(){}
    });
    

    you will need to get the array from value "SearchFormInput", and then parse it. If you don't need to have this you can send just the array. Something like:

    var stringArray = JSON.stringify(search_form_data_array);
    
    ...,
    data: stringArray,
    ...,
    
    点赞 评论

相关推荐