在android中动态生成JSONObject

在android中动态生成JSONObject,android,json,Android,Json,我想生成以下表单 { "dt": { "DocumentElement": [ { "CompanyID": "8", "Question": "Who I M?", "Answer": "dfsfdsfd" }, { "CompanyID": "8", "Questi

我想生成以下表单

{
"dt": {
    "DocumentElement": [

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

        },
        {

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

        }
    ]
  }
}
我有一个动态填充数据的arraylist,我还希望表单是动态的。这是我的密码:

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

答案按顺序排在第一位,但我想先回答CompanyID,那有什么问题呢?

有一个非常好的谷歌库,名为gson,它可以帮助您轻松地创建Json对象或解析Json对象

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

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

编辑答案:-

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

}
希望这对您有所帮助。

您忘记将JSONObject
reqObj
添加到JSONArray
req
中了。如
req.put(reqObj)

从循环的
中修改代码块

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

您不需要将reqObj添加到req

Do
REQU.put(REQUOBJ)

JSONObject documentElementobj=新的JSONObject();
JSONArray req=新的JSONArray();
试一试{
对于(int i=0;i
此外,通常最好让变量以小写字母开头


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

使用以下代码填写您的答案:

        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"));
        }
JSONObject jObj=新的JSONObject(“您的web响应”);
JSONObject jObj1=jObj.getJSONObject(“dt”);
JSONArray item=jObj.getJSONArray(“DocumentElement”);
对于(int i=0;i
您如何处理req?没有什么?您向reqObj添加了一些内容,但没有对其进行任何处理,这并不能真正回答问题。我在循环中添加了req.put(reqObj),但每次都会使用相应的arraylist sizeMove
JSONObject reqObj=new JSONObject(),获取最后一个arraylist数据进入for循环。看我的answer@HarshalKalavadiya您应该真正考虑调试器的使用,特别是如果您使用Eclipse/Android Studio开发的话。你会很容易发现这些问题。@PankajKumar:请看我的编辑块。你无法控制属性的顺序。它不会影响您的JSON。在解析时,这将与最初的方式相同。所以不要担心顺序,你不必关心这个。它不会改变你解析的方式。接受一个答案,因为它可能对其他人有用。
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?"}]}}
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
    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();
    }
        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"));
        }