简体   繁体   English

返回JSON数组的正确方法是什么?

[英]What is the right way of returning a JSON array?

I'm having a method that returns a JSON array here is my method 我有一个返回JSON数组的方法,这是我的方法

 public net.sf.json.JSONArray getquery(QueryBuilderRequestHelper helper) {
     //logger.log(Level.INFO, " ############### getquery ###############  ");
        net.sf.json.JSONObject jObject = new net.sf.json.JSONObject();
        net.sf.json.JSONArray jArray = new net.sf.json.JSONArray();
        net.sf.json.JSONObject tempJsonObject = null;
        QueryBuilderJpaController builderJpaController = new QueryBuilderJpaController();
        try {
            tempJsonObject =new net.sf.json.JSONObject();
            QueryBuilder builder=builderJpaController.findByqueryId(Integer.parseInt(helper.getQueryId()));
            String outputFields=builder.getOutputFields();
            String qbCondition=builder.getQbCondition();
            tempJsonObject.put("qbCondition", qbCondition = qbCondition.replaceAll("^\"|\"$", ""));

             jArray.add(tempJsonObject);
             List<String> list = new ArrayList<String>();
             list = (List<String>) net.sf.json.JSONArray.toCollection(net.sf.json.JSONArray.fromObject(outputFields));
             for (String string : list) {

                tempJsonObject.put("outputFields", string);
                jArray.add(tempJsonObject);
            }
            List<QueryBuilderCondition>  tbdAnsList = builderJpaController.getQueryBuilderConditionByQueyId(Integer.valueOf(Integer.parseInt(helper.getQueryId())));
         for (QueryBuilderCondition queryBuilderCondition : tbdAnsList) {

             tempJsonObject.put("fieldId", queryBuilderCondition.getFieldId());
             tempJsonObject.put("operator", queryBuilderCondition.getOperator());
             tempJsonObject.put("fieldValue", queryBuilderCondition.getFieldValue());
             jArray.add(tempJsonObject);
            }

        }catch(Exception e){
            e.printStackTrace();
        }
        return jArray;
 }

Please let me know is this the right way of returning a JSON array if this correct please tell while iterating this on javascript like 请让我知道这是返回JSON数组的正确方法,如果此正确方法在javascript上进行迭代时请告知

success: function(data) {

        for(var i in data)
        {   
             var qbCondition = data.qbCondition;
             console.log("======qbCondition is =="+JSON.stringify(qbCondition))
             var outputFields = data[i].outputFields;
             console.log("======outputFields is =="+JSON.stringify(outputFields))
             var fieldId = data[i].fieldId;
             console.log("======fieldId is =="+JSON.stringify(fieldId))
             var operator = data[i].operator;
             console.log("======operator is =="+JSON.stringify(operator))
             var fieldValue = data[i].fieldValue;
             console.log("======fieldValue is =="+JSON.stringify(fieldValue))

        }

Why I got undefine during its first iteration.. Thank You 为什么我在第一次迭代时不确定。.谢谢

I would advice you to return a String corresponding to the JSON Array stringified in your java method. 我建议您返回一个与java方法中字符串化的JSON数组相对应的String

Your json might look like that: 您的json可能看起来像这样:

{"qbCondition":"myCondition", "data":[
    {"outputFields" : "myOutputFields1", "fieldId" : "fieldId1" },
    {"outputFields" : "myOutputFields2", "fieldId" : "fieldId"}
]}

Then on the javascript part, you can retrieve the JSON object with JSON.parse(object) : 然后在javascript部分,您可以使用JSON.parse(object)检索JSON对象:

success: function(response) {
    var jsonData = JSON.parse(response);
    var qbCondition = jsonData.qbCondition;
    console.log("======qbCondition is =="+qbCondition);

    // get your array and display index 0
    var myArr = jsonData.data;
    var outputFields = myArr[0].outputFields;
    console.log("======outputFields is=="+outputFields);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM