如何将JSON数组API存储在SQLite数据库中。

huangapple go评论127阅读模式
英文:

How to store json arrays api in sqlite db

问题

[
{
"id": "1",
"name": "单人床单"
},
{
"id": "2",
"name": "双人床单"
},
{
"id": "3",
"name": "单人床罩"
},
{
"id": "4",
"name": "双人床罩"
},
{
"id": "5",
"name": "枕套"
},
{
"id": "6",
"name": "浴巾"
},
{
"id": "7",
"name": "手巾"
},
{
"id": "8",
"name": "浴垫"
},
{
"id": "9",
"name": "黄色床单"
},
{
"id": "10",
"name": "彩色毛巾"
}
]

英文:
[
  {
    "id": "1",
    "name": "Single Bed Sheet"
  },
  {
    "id": "2",
    "name": "Double Bed Sheet"
  },
  {
    "id": "3",
    "name": "Single Bed Cover"
  },
  {
    "id": "4",
    "name": "Double Bed Cover"
  },
  {
    "id": "5",
    "name": "Pillow Cover"
  },
  {
    "id": "6",
    "name": "Bath Towel"
  },
  {
    "id": "7",
    "name": "Hand Towel"
  },
  {
    "id": "8",
    "name": "Bath Mat"
  },
  {
    "id": "9",
    "name": "Yellow Bed Sheet"
  },
  {
    "id": "10",
    "name": "Color Towel"
  }
]

答案1

得分: 0

Create POJO class from json and store it in SQLite

从 `json` 创建 POJO 类并存储在 `SQLite` 中

Using Gson, parse the jsonArray to List<ApiResponse>

使用 `Gson`,将 `jsonArray` 解析为 `List<ApiResponse>`

Now loop through the list and store it into the database

现在通过循环遍历列表并将其存储到数据库中
英文:

Create POJO class from json and store it in SQLite

class ApiResponse {
    private String id;
    private String name;

    //getter-setter

}

Using Gson, parse the jsonArray to List<ApiResponse>

Type listType = new TypeToken<List<ApiResponse>>() {}.getType();
List<ApiResponse> apiResponses = new Gson().fromJson(yourJson, listType);

Now loop through the list and store it into database

for(ApiResponse response: apiResponses) {
    //insert response into database
}

答案2

得分: 0

以下是翻译好的部分:

所以,您正在显示一个'对象'数组。这些对象具有诸如'id'和'name'之类的属性。通常情况下,您会创建一个具有这些属性的类对象。然后,当您检索JSON数组时,您会遍历该数组并创建一个新的类对象实例,并从JSON数组中填充它。然后,您将将您的类实例添加到对象数组中。

当您想要将它们添加到SQLITE数据库时,您可能希望遍历您的类对象数组,并将每个实例发送到一个方法以将数据插入SQLITE数据库中。以下是将对象数据插入SQLITE数据库的示例:

//---向数据库插入新的位置---
public long insertLocation(LocationAddress location)
{

if (myDataBase == null) {
	openDataBase();
}

ContentValues initialValues = new ContentValues();
initialValues.put("name", location.getName());
initialValues.put("address", location.getAddress());
initialValues.put("latitude", location.getLatitude());
initialValues.put("longitude", location.getLongitude());
initialValues.put("type", location.getType());
initialValues.put("sort_order", location.getSortOrder());

return myDataBase.insert("locations", null, initialValues);

}

还有许多其他与使用SQLITE数据库相关的内容,例如在第一次使用时从Assets复制它,打开数据库等。您可以搜索DataBaseHelper.java以获取一个包含许多这些方法的实用程序文件的示例,可供您使用。

英文:

So, you are showing an array of 'objects'. Those objects have properties such as 'id' and 'name'. What you would normally do, is create a class object with those properties. Then, when you retrieve the JSON Array, you would iterate through that array and create a new instance of your class object and populate it from the JSON Array. Then, you would add your class instance to an array of objects.

When you want to add them to SQLITE db, you might want to iterate through your class object array and send each instance up to a method to insert the data into the SQLITE database. Here is an example of inserting data from an object into a SQLITE database:

	//---insert a new Location into the database---
public long insertLocation(LocationAddress location) 
{
	
	if (myDataBase == null) {
		openDataBase();
	}
	
	ContentValues initialValues = new ContentValues();
	initialValues.put("name", location.getName());
	initialValues.put("address", location.getAddress());
	initialValues.put("latitude", location.getLatitude());
	initialValues.put("longitude", location.getLongitude());
	initialValues.put("type", location.getType());
	initialValues.put("sort_order", location.getSortOrder());
	

	return myDataBase.insert("locations", null, initialValues);

}

There is a lot of other stuff related to using a SQLITE database, such as copying it from Assets on the first use, opening the database. You should Google DataBaseHelper.java for examples of a utility file that has a lot of these methods ready for you to use.

huangapple
  • 本文由 发表于 2020年1月4日 00:23:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定