0

到目前为止我所做的是:我创建了一个这种格式的 json 对象:

{ "checklist": {
"id": "1", "name": "shoppping", "desc":"description", "is_editable":"false", "item": [ {"id":"3","value": "dfdf", "desc": "wwe"}, {"id":"3","value": "dfdf", "desc": "wwe"}, {"id":"3","value": "dfdf", "desc": "wwe"} ] } }

使用以下代码:

public static void sendjsontourl(String id) throws JSONException, IllegalStateException, IOException
{
 String title;
 String desc;
 String creator;
 Boolean is_editable;

 String[] itid;
 String[] itname;
 String[] itdesc;
 Boolean[] itchk;
 Integer count;

 title=DBInterface.getlistname(id);
 desc=DBInterface.getlistdesc(id);
 creator=DBInterface.getlistcreartor(id);
 is_editable=DBInterface.geteditablemode(id);     
 itid=DBInterface.getitemid(id);
 itname=DBInterface.getitemname(id);
 itdesc=DBInterface.getitemdesc(id);
 itchk=DBInterface.readdataitem(id);
 count=DBInterface.getitemcount(id);     

 JSONObject checklist = new JSONObject();
 JSONObject obj = new JSONObject();
 JSONArray item = new JSONArray();
 JSONObject reqObj = new JSONObject();   
 for(int i=0; i<count; i++)
 {

    reqObj.put( "id",""+itid[i]);
    reqObj.put( "value",""+ itname[i]);
    reqObj.put( "desc", ""+itdesc[i] );
    item.put( reqObj );
 }        
    obj.put( "item", item );
    obj.put("id",""+id);
    obj.put("name",""+title);
    obj.put("description",""+desc);
    obj.put("creator",""+creator);
    obj.put("is_editable",""+is_editable);      
    checklist.put("checklist",obj);
    Log.d("log_tag",String.valueOf(checklist));
}   

我的问题:您可以看到包含 json 数组项的 json 对象..每个项目的项目值都是相同的...从 sqlite 数据库中获取数据的部分是正确的..我不知道是什么问题以及为什么最后一个索引值只存储在 json 数组中!

4

1 回答 1

1

您正在为 reqObj 使用相同的 JSONObject 实例,因此您覆盖了数据编辑:

for(int i=0; i<count; i++)
 {
    JSONObject reqObj = new JSONObject();
    reqObj.put( "id",""+itid[i]);
    reqObj.put( "value",""+ itname[i]);
    reqObj.put( "desc", ""+itdesc[i] );
    item.put( reqObj );
 }      
于 2013-02-04T12:54:58.287 回答