我是android的初学者,编写简单的应用程序将json数组发送到服务器,编写此代码:
JSONArray jsonArray = new JSONArray();
JSONObject obj = new JSONObject();
String DATABASE_NAME = "TEMPFOOD";
String TABLE_NAME = "tempData";
try{
SQLiteDatabase mydb = openOrCreateDatabase(DATABASE_NAME, TempActivity.MODE_PRIVATE,null);
Cursor allrows = mydb.rawQuery("SELECT * FROM "+ TABLE_NAME, null);
if(allrows.moveToFirst()){
do{
String Food_Name = allrows.getString(0);
String Value = allrows.getString(1);
String NOBAT = allrows.getString(2);
String TIME = allrows.getString(3);
String DATE = allrows.getString(4);
try {
obj.put("Food_Name", Food_Name)
.put("Value", Value)
.put("NOBAT", NOBAT)
.put("TIME", TIME)
.put("DATE", DATE);
} catch (JSONException e) {
e.printStackTrace();
}
jsonArray.put(obj);
}
while(allrows.moveToNext());
}
mydb.close();
}catch(Exception e){
//Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
}
String jsonText = jsonArray.toString();
例如,我从sqlite
读取4条记录,保存到jsonArray
最新记录中,为什么?我怎么解决?谢谢。
答案 0 :(得分:2)
它只存储最后一条记录,因为您没有在JSONObject
循环中创建新的do...while
。请参阅我编辑的代码。
JSONArray jsonArray = new JSONArray();
JSONObject obj = new JSONObject();
String DATABASE_NAME = "TEMPFOOD";
String TABLE_NAME = "tempData";
try{
SQLiteDatabase mydb = openOrCreateDatabase(DATABASE_NAME, TempActivity.MODE_PRIVATE,null);
Cursor allrows = mydb.rawQuery("SELECT * FROM "+ TABLE_NAME, null);
if(allrows.moveToFirst()){
do{
obj = new JSONObject();
String Food_Name = allrows.getString(0);
String Value = allrows.getString(1);
String NOBAT = allrows.getString(2);
String TIME = allrows.getString(3);
String DATE = allrows.getString(4);
try {
obj.put("Food_Name", Food_Name)
.put("Value", Value)
.put("NOBAT", NOBAT)
.put("TIME", TIME)
.put("DATE", DATE);
} catch (JSONException e) {
e.printStackTrace();
}
jsonArray.put(obj);
}
while(allrows.moveToNext());
}
mydb.close();
}catch(Exception e){
//Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
}
String jsonText = jsonArray.toString();
答案 1 :(得分:0)
显示使用Gson
库
制作Record.java
课程
import com.google.gson.annotations.SerializedName;
public class Record {
@SerializedName("Name")
private String name;
@SerializedName("password")
private String password;
@SerializedName("item1")
private String item1;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getItem1() {
return item1;
}
public void setItem1(String item1) {
this.item1 = item1;
}
}
对此RecordTestDrive.java
import java.util.ArrayList;
import com.google.gson.Gson;
public class RecordTestDrive {
public static void main(String[] args) {
ArrayList<Record> records = new ArrayList<>();
for (int i = 0; i < 4; i++) {
Record record = new Record();
record.setName("Name_"+i);
record.setPassword("password_"+i);
record.setItem1("item_"+i);
records.add(record);
}
String jsonResult = (new Gson()).toJson(records);
System.out.println(""+jsonResult);
}
}
输出==&GT;
[
{
"Name": "Name_0",
"password": "password_0",
"item1": "item_0"
},
{
"Name": "Name_1",
"password": "password_1",
"item1": "item_1"
},
{
"Name": "Name_2",
"password": "password_2",
"item1": "item_2"
},
{
"Name": "Name_3",
"password": "password_3",
"item1": "item_3"
}
]
以这种方式,一旦您将数据从SQLite
数据库读入对象,之后您可以轻松地从数据库中收集的数据中生成JSON
字符串数组,以将其发送到服务器