使用sqlite数据库中的值创建json数组时出现问题

时间:2013-02-04 12:13:32

标签: android json

到目前为止我所做的是:我创建了一个这种格式的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数组中!

1 个答案:

答案 0 :(得分: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 );
 }