【问题标题】:how to iterate json on servlet如何在servlet上迭代json
【发布时间】:2016-08-03 04:39:53
【问题描述】:

我有像 json 一样的

{
"condition": "AND",
"rules": [{
    "id": "BirthDate",
    "field": "BirthDate",
    "type": "date",
    "input": "text",
    "operator": "equal",
    "value": "2016/04/13"
}]}

我只想在我创建的 servlet 上对其进行迭代

public String getRuleList(){
    String ruleList=this.get("rules");
    return ruleList;
}
public String getcondition(){
    return this.get("condition");
}

当我在不使用 JSON.stringify 的情况下发送此 json 时,作为 getter setter,我得到了条件值,但无法获取规则。通过使用 JSON.stringify,我无法获取任何内容。请帮忙..

【问题讨论】:

    标签: java json ajax


    【解决方案1】:

    rules 是 JSONArray,简单的 get 返回一个对象。尝试使用 json 库自定义方法来获取它。 如果你使用org.json,试试这个

    this.getJSONArray('rules')

    尝试提供更多信息,例如您正在使用的 json 库、您为 this.get('rules') 获得的输出以及如何初始化 this 以获得更好的答案

    【讨论】:

    • this.get('rules') 给我 null
    • 我的ajax调用就像var url = location.protocol + "//" + location.host +appContext+"?command=QueryBuilderServlet&action=getQueryJson"; $.ajax({ url:url, type: "POST", data:JSON.stringify(result), dataType:'json', contentType : 'application/json' });
    • 参考这个question
    【解决方案2】:

    为了解决上述问题,我做了

    result =JSON.stringify(result); var json = JSON.parse(result); var queryData={ rules:JSON.stringify(json.rules), condition:JSON.stringify(json.condition) }; console.log("result"+JSON.stringify(result)); if (!$.isEmptyObject(result)) { var url = location.protocol + "//" + location.host +appContext+"?command=QueryBuilderServlet&action=getQueryJson"; $.ajax({ url:url, type: "POST",
    data:queryData, dataType:'json', });
    非常感谢您的帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      • 2014-09-24
      • 2017-05-21
      • 2019-11-19
      • 2016-08-03
      • 2019-07-21
      • 1970-01-01
      相关资源
      最近更新 更多