如何在couchbase(Android)中迭代存储和检索包含嵌套JSON对象的JSONArray

时间:2018-02-05 11:56:21

标签: java android json couchbase-lite couchbase-view

尝试在Android中使用此代码将JSON array存储到Couchbase,将其置于失败状态。请帮助您如何成功存储它以及如何迭代接收所有数据。查看Couchbase文档,但仍然卡住了。在我能够存储和接收之前,我是否必须将JSON array转换为对象?或者从API

处理此类数据的正确方法是什么
private void storeLocalData() {

        //COUCHBASE IMPLEMENTATION
        // Create a manager
        manager = null;
        try {
            manager = new Manager(new AndroidContext(getActivity()), Manager.DEFAULT_OPTIONS);
        } catch (IOException e) {
            e.printStackTrace();
        }


        // Create or open the database named 'saved_project_sample'
        database = null;
        try {
            database = manager.getDatabase("saved_project_sample");
        } catch (CouchbaseLiteException e) {
            e.printStackTrace();
        }

        // Create a new document
        document = database.createDocument();


        try {
            //Cast json String into JSON Array
            JSONArray jsonArray= new JSONArray(json);

            HashMap<String, Object> properties = new HashMap<>();

                for (int k = 0; k < jsonArray.length(); k++) {
                    JSONObject objJson = jsonArray.getJSONObject(k);

                    Object id = objJson.getString("id");
                    Object title = objJson.getString("title");
                    Object owner_id = objJson.getString("owner_id");
                    Object description = objJson.getString("description");
                    Object status = objJson.getString("status");
                    Object start_time = objJson.getString("start_time");
                    Object finish_time = objJson.getString("finish_time");
                    Object created = objJson.getString("created");
                    Object modified = objJson.getString("modified");

                    properties.put("id", id);
                    properties.put("title", title);
                    properties.put("owner_id", owner_id);
                    properties.put("description", description);
                    properties.put("status", status);
                    properties.put("start_time", start_time);
                    properties.put("finish_time", finish_time);
                    properties.put("created", created);
                    properties.put("modified", modified);


                    document.putProperties(properties);
                Toast.makeText(getActivity(), "Successful Storage", Toast.LENGTH_SHORT).show();
                }


        } catch (CouchbaseLiteException e) {
            e.printStackTrace();
            Toast.makeText(getActivity(), "Unsuccessful", Toast.LENGTH_SHORT).show();
        } catch (JSONException e) {
            e.printStackTrace();
            Toast.makeText(getActivity(), "Unsuccessful", Toast.LENGTH_SHORT).show();
        }

    }

这是我的JSON数组

[
    {
        "id": "1",
        "title": "New API",
        "owner_id": "dsdssdsd445d",
        "description": "Yh A Testin API",
        "status": "unactiveAPI",
        "start_time": "2017-08-01 14:25:22.060000",
        "finish_time": "2017-08-01 14:25:22.060000",
        "created": "2017-08-01 14:25:22.060000",
        "modified": "2017-08-01 14:25:22.060000"
    },
    {
        "id": "2",
        "title": "New TW Projec API",
        "owner_id": "dsdssdsd445d",
        "description": "Testin API",
        "status": "unactiveAPI",
        "start_time": "2017-08-01 14:25:22.060000",
        "finish_time": "2017-08-01 14:25:22.060000",
        "created": "2017-08-01 14:25:22.060000",
        "modified": "2017-08-01 14:25:22.060000"
    },
    {
        "id": "3",
        "title": "Projec API",
        "owner_id": "dsdssdsd445d",
        "description": "Testin",
        "status": "unactiveAPI",
        "start_time": "2017-08-01 14:25:22.060000",
        "finish_time": "2017-08-01 14:25:22.060000",
        "created": "2017-08-01 14:25:22.060000",
        "modified": "2017-08-01 14:25:22.060000"
    }
]

2 个答案:

答案 0 :(得分:2)

使用GSON和一个类,然后您不需要手动获取每个变量。

public class UserDocument{
    public int id;
    public String title;
    public String owner_id;
    public String description;
    public String status;
    public Date start_time;
    public Date finish_time;
    public Date created;
    public Date modified;

    public UserDocument() {}; //Needs an empty constructor for reflection;
}

现在你有了这个课程,那么你可以写

UserDocument[] userDocuments = new Gson().fromJson(json, UserDocument[].class);

for(UserDocument userDocument : userDocument){

    HashMap<String, Object> properties = new HashMap<>();

    properties.put("id", userDocument.id);
    properties.put("title", userDocument.title);
    properties.put("owner_id", userDocument.owner_id);
    properties.put("description", userDocument.description);
    properties.put("status", userDocument.status);
    properties.put("start_time", userDocument.start_time);
    properties.put("finish_time", userDocument.finish_time);
    properties.put("created", userDocument.created);
    properties.put("modified", userDocument.modified);

    try {
        document.putProperties(properties);
    } catch (CouchbaseLiteException e) {
        e.printStackTrace(); //This will tell you why it fails
    }
}

只需定义属性的类型,然后GSON将检查属性是否可以成为该类型

答案 1 :(得分:0)

不可能将hashmap的多个属性放入奇异的Couchbase doucment中。

您每次都可以

创建一个新文档
document = database.createDocument(); 

或者

 document = database.getDocument(String documentId);

document.putProperties(properties);

我的代码现在可以正常运行而不会崩溃

  ProjectSummary[] projectSummaries = new Gson().fromJson(json, ProjectSummary[].class);
        int project_number=1;
        HashMap<String, Object> properties = new HashMap<>();

        for (ProjectSummary projectSummary : projectSummaries) {


            // Create a new document - Every Project-List Will Be Its Own Document
            document = database.getDocument("Project-"+ project_number);

            properties.put("id", projectSummary.id);
            properties.put("title", projectSummary.title);
            properties.put("owner_id", projectSummary.owner_id);
            properties.put("description", projectSummary.description);
            properties.put("status", projectSummary.status);
            properties.put("start_time", projectSummary.start_time);
            properties.put("finish_time", projectSummary.finish_time);
            properties.put("created", projectSummary.created);
            properties.put("modified", projectSummary.modified);

        try {
            document.putProperties(properties);
            Toast.makeText(context, project_number+"- ID Saved", Toast.LENGTH_SHORT).show();
        } catch (CouchbaseLiteException e) {
            e.printStackTrace(); //This will tell you why it fails
            Toast.makeText(context, "failure", Toast.LENGTH_SHORT).show();
        }

        //Increase Project Number
        project_number++;

    }

    Toast.makeText(context, "Project Download Successful \n" , Toast.LENGTH_SHORT).show();

}

Hashmap Mapping的ProjectSummary类

public class ProjectSummary {
public String id;
public String title;
public String owner_id;
public String description;
public String status;
public String start_time;
public String finish_time;
public String created;
public String modified;


public ProjectSummary() {
}