0

i have json like

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

i just want to iterate it on servlet for that i create

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

as getter setter when i send this json without using JSON.stringify i got the value of condition but unable to fetch rules.By using JSON.stringify i unable to fetch anything. please help..

balazs630
  • 3,421
  • 29
  • 46
Monu Mittal
  • 95
  • 1
  • 1
  • 9

2 Answers2

0

rules is JSONArray and simple get returns an object. Try to get it with the json libraries custom method. If you are using org.json try this

this.getJSONArray('rules')

Try to provide some more information such as the json library you are using, the output you got for this.get('rules') and how is this initialized for a much better answer

kryshna
  • 113
  • 1
  • 6
  • this.get('rules') gives me null – Monu Mittal Apr 12 '16 at 14:24
  • my ajax call is like `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' });` – Monu Mittal Apr 12 '16 at 14:28
  • refer to this [question](http://stackoverflow.com/questions/5338943/read-json-string-in-servlet) – kryshna Apr 12 '16 at 14:28
0

To solve above problem i do

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', });
thanks a lot for your help

Monu Mittal
  • 95
  • 1
  • 1
  • 9