0

I try to save properties in a Couchbase-document in Android. The properties hold a JSONArray with several JSONObjects.

When I do document.putproperties(myproperties) a couchbaseliteexception with state 400 and the message "bad or missing json" is thrown".

So the JSONArray looks like:

"some_prop" -> "[
{
    "content":"someContent",
    "key2":"",
    "key3":"",
    "key4":"",
    "month":8,
    "day":3,
    "key5":115
},
{
    "content":"Some other content",
    "key2":"something",
    "key3":"",
    "key4":"",
    "month":8,
    "day":3,
    "key5":115
}]"

Can anyone tell me whats the problem with this JSON?

EDIT: the JSONArray with the corresponding key is saved in a hashmap like it is explained in: http://developer.couchbase.com/mobile/develop/guides/couchbase-lite/native-api/document/index.html

EDIT 2: The Method where the update is executed and the JSONArray is filled:

private void updateDoc(ArrayList<MyObject> objects) {
    Document document = getDocument();

    // Update the document with more data
    Map<String, Object> updatedProperties = new HashMap<>();

    JSONArray objectArray = new JSONArray();

    //fill array with data
    for(MyObject element : objects) {
        JSONObject jsonObjects = element.toJSONObject();
        if(jsonObjects != null) {
            objectArray.put(jsonObjects);
        }
    }

    //set data to property map
    updatedProperties.put(MYOBJECT_PROP_IDENTIFIER, objectArray);

    try {
        // Save properties to the Couchbase local Couchbase Lite DB
        document.putProperties(updatedProperties);
    } catch (CouchbaseLiteException e) {
    }
}
e_card
  • 119
  • 2
  • 10
  • 1
    I think "some_prop" -> " should be like "some_prop" : " – Remees M Syde Sep 02 '15 at 09:18
  • i just got it like this out of the debugger from Android Studio. In couchbase the properties are saved in a hashmap. so up added the JSONArray in the map through myproperties.put("some_prop", myJSONArray). I followed the guide in: http://developer.couchbase.com/mobile/develop/guides/couchbase-lite/native-api/document/index.html – e_card Sep 02 '15 at 09:41

2 Answers2

0

Not sure if this is what you're looking for,

You can also use something like http://jsonlint.com to verify your son

{
    "some_prop": [
        {
            "content": "someContent",
            "key2": "",
            "key3": "",
            "key4": "",
            "month": 8,
            "day": 3,
            "key5": 115
        },
        {
            "content": "Some other content",
            "key2": "something",
            "key3": "",
            "key4": "",
            "month": 8,
            "day": 3,
            "key5": 115
        }
    ]
}
Hades
  • 3,916
  • 3
  • 34
  • 74
0

Finally I found a solution to the problem: I don't use JSONObject or JSONArray anymore but save my data in ArrayList and put each element directly into the database. so i don't have an Array with all the elements which but a lot of single elements direktly in the document. To accesss them later I save also the number of elements in the DB. Each element has the index as a prefix so it can be identified later on. That's not quite a nice way but it works..

e_card
  • 119
  • 2
  • 10