1

我想生成以下表格

{
"dt": {
    "DocumentElement": [

            {
                "CompanyID": "8",
                "Question": "Who I M?",
                "Answer": "dfsfdsfd"

        },
        {

                "CompanyID": "8",
                "Question": "Who I M?",
                "Answer": "Chintan"

        }
    ]
  }
}

我有一个动态填充数据的数组列表,我也想要动态的形式。这是我的代码:

JSONObject DocumentElementobj = new JSONObject();
        JSONArray req = new JSONArray();

        JSONObject reqObj = new JSONObject();
        try {
            for (int i = 0; i < OnLineApplication.mParserResults.size(); i++) {


                reqObj.put("CompanyID", "8");
                reqObj.put("Question",OnLineApplication.mParserResults.get(i).getQuestion());
                reqObj.put("Answer",OnLineApplication.mParserResults.get(i).getAnswer());

            }


            DocumentElementobj.put( "DocumentElement", req );
            System.out.println("Final "+DocumentElementobj.toString());
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

它输出: Final {"DocumentElement":[]}

编辑

感谢您的所有回复。根据你们所有人的回应,我编写如下代码

JSONObject DocumentElementobj = new JSONObject();
        JSONArray req = new JSONArray();
        JSONObject reqObjdt = new JSONObject();

        try {
            for (int i = 0; i < OnLineApplication.mParserResults.size(); i++) {

                JSONObject reqObj = new JSONObject();
                reqObj.put("CompanyID",   OnLineApplication.mParserResults.get(i).getCompanyId());
                reqObj.put("Question",OnLineApplication.mParserResults.get(i).getQuestion());
                reqObj.put("Answer",OnLineApplication.mParserResults.get(i).getAnswer());
                req.put(reqObj);


            }



            DocumentElementobj.put( "DocumentElement", req );
            reqObjdt.put("dt", DocumentElementobj);
            System.out.println("Final "+reqObjdt.toString());
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

我得到了我想要的foramt,但在最后的字符串中我得到了如下所示的序列

{"dt":
  {"DocumentElement":
    [
    {"Answer": "The Claims Representatives have a small role in return to  work.","Question":"Return-to-Work Claim Issues. Please check the statement that best  applies.","CompanyID":"8"},
    {"Answer":"Poor","Question":"How would you describe the level of your general employee’s understanding of the impact of workers’ compensation costs on your organization?","CompanyID":"8"}]}}

它按顺序首先回答,但我想先回答 CompanyID,那有什么问题?

4

4 回答 4

1

有一个非常好的 Google 库,叫做 gson,它可以帮助您轻松创建 Json 对象或解析 Json 对象。

您需要在项目中添加 gson.jar 文件。

有关更多详细信息,请访问以下链接。

https://sites.google.com/site/gson/gson-user-guide

编辑答案:-

public class DocumentElement {
    @Expose
    private String CompanyID;
    @Expose
    private String Question;
    @Expose
    private String Answer;

    public DocumentElement(String CompanyID, String Question, String Answer) {
        this.CompanyID = CompanyID;
        this.Question = Question;
        this.Answer = Answer;
    }

    public String getCompanyID() {
        return CompanyID;
    }

    public String getQuestion() {
        return Question;
    }

    public String getAnswer() {
        return Answer;
    }

}

public class Data {
    @Expose
    private ArrayList<DocumentElement> DocumentElement;

    public ArrayList<DocumentElement> getDocumentElement() {
        return DocumentElement;
    }

    public void setDocumentElement(ArrayList<DocumentElement> DocumentElement) {
        this.DocumentElement = DocumentElement;
    }

}


public class ParentData {
    @Expose
    private Data dt;

    public Data getDt() {
        return dt;
    }

    public void setDt(Data dt) {
        this.dt = dt;
    }

}

并像这样在 gson.jar 的帮助下创建 JsonObject

ArrayList<DocumentElement> doc=new ArrayList<DocumentElement>();
        DocumentElement doc1=new DocumentElement("8", "Who I M?", "Amit");
        DocumentElement doc2=new DocumentElement("9", "Who I M?", "Gupta");
        doc.add(doc1);
        doc.add(doc2);
        Data data=new Data();
        data.setDocumentElement(doc);
        ParentData parent=new ParentData();
        parent.setDt(data);
        Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
        String jsonObj = gson.toJson(parent);
        System.out.println("createdJson:---"+jsonObj);

结果是

{"dt":{"DocumentElement":[{"Answer":"Amit","CompanyID":"8","Question":"Who I M?"},{"Answer":"Gupta","CompanyID":"9","Question":"Who I M?"}]}}

希望这会帮助你。

于 2013-10-11T06:16:43.897 回答
1

您忘记将JSONObject reqObj添加到JSONArray req中。喜欢req.put(reqObj);

for loop修改您的代码块

JSONObject reqObj = new JSONObject(); // Move inside the loop
reqObj.put("CompanyID", "8");
reqObj.put("Question",OnLineApplication.mParserResults.get(i).getQuestion());
reqObj.put("Answer",OnLineApplication.mParserResults.get(i).getAnswer());
req.put(reqObj); // ADDED HERE
于 2013-10-11T06:20:59.633 回答
1

您不会将 reqObj 添加到 req。

req.put(reqObj)

    JSONObject documentElementobj = new JSONObject();
    JSONArray req = new JSONArray();


    try {
        for (int i = 0; i < OnLineApplication.mParserResults.size(); i++) {

            JSONObject reqObj = new JSONObject();
            reqObj.put("CompanyID", "8");
            reqObj.put("Question",OnLineApplication.mParserResults.get(i).getQuestion());
            reqObj.put("Answer",OnLineApplication.mParserResults.get(i).getAnswer());
            req.put(reqObj);
        }


        documentElementobj.put( "documentElement", req );
        System.out.println("Final "+ documentElementobj.toString());
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

此外,通常最好让变量以小写字母开头。

还有一件事,在这种情况下使用调试器将是有效的。

于 2013-10-11T06:21:07.723 回答
0

使用此代码来填写您的答案:

        JSONObject jObj = new JSONObject("Your web Response");

        JSONObject jObj1 = jObj.getJSONObject("dt");

        JSONArray   item = jObj.getJSONArray("DocumentElement");

        for (int i = 0; i < item.length(); i++) 
        {
            jObj_data = (JSONObject) item.get(i);
             reqObj.put("CompanyID", jObj_data.getString("CompanyID"));
             reqObj.put("Question",jObj_data.getString("Question"));
             reqObj.put("Answer",jObj_data.getString("Answer"));
        }
于 2013-10-11T06:33:52.427 回答