简体   繁体   中英

How to iteratively store and retrieve JSONArray which contains nested JSON objects in couchbase (Android)

Trying to store a JSON array into Couchbase using this code in Android, put it is unsuccessful. Please help with how to successfully store it and also how to iteratively receive all data. Looked through the Couchbase documentation, but still stuck. Do i have to convert the JSON array to an object before i would be able to store and receive? Or what is the right way to handle such data from an 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();
        }

    }

This is my JSON Array

[
    {
        "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"
    }
]

Use GSON and a class, then you do not need to manually get each variable.

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;
}

Now you have the class, then you can write

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
    }
}

Just define the type of the property, then GSON will check if the property can become that type

It is impossible to put multiple properties of a hashmap into a singular Couchbase doucment.

You create a new document everytime by

document = database.createDocument(); 

Or

 document = database.getDocument(String documentId);

Before

document.putProperties(properties);

My code below now works properly without crashing

  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();

}

ProjectSummary Class for Hashmap Mapping

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() {
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM