delight-im / Android-DDP

[UNMAINTAINED] Meteor's Distributed Data Protocol (DDP) for clients on Android
Apache License 2.0
274 stars 54 forks source link

How can I manage a json of this way? #94

Closed cristianhoyos66-zz closed 8 years ago

cristianhoyos66-zz commented 8 years ago

We guess that i'm expecting a json of this way: {info: {coordinates: [12345, -12345], name: 'coordinateName'}} how can I manage that in onDataAdded method?

I'm testing this way to get coordinates:

Database database = mMeteor.getDatabase();
 public void onDataAdded(String collectionName, String documentID, String newValuesJson) {
    JSONObject infoField =  (JSONObject) database.getCollection(collectionName).getDocument(documentId).getField("info");
    JSONArray coor = infoField.getJSONArray("coordinates"); 
   }

How can i manage that? or is this a right way?

thank you.

ocram commented 8 years ago

Thanks for your question!

It seems you had the correct idea already. This is how you query the database, in case you have activated it as described in the README:

database.getCollection(collectionName);
database.getCollection(collectionName).getDocument(documentId);
database.getCollection(collectionName).getDocument(documentId).getField("info");

No need to convert to JSONObject!

You can also check what is in there by printing the contents to your console as follows:

System.out.println(database.getCollection(collectionName));
System.out.println(database.getCollection(collectionName).getDocument(documentId));
System.out.println(database.getCollection(collectionName).getDocument(documentId).getField("info"));

Does that help?

cristianhoyos66-zz commented 8 years ago

Yes, I understand that, thank you, but to get coordinates How can I do that?

I tried this: database.getCollection(collectionName).getDocument(documentId).getField("coordinates"); and it returns null so I think that I need to get first info field to can get coordinates field, but if I do this:

Object infoField = database.getCollection(collectionName).getDocument(documentId).getField("info");

I can't get coordinates from infoField so I don't know how could I get coordinates field.

Thanks for answering.

ocram commented 8 years ago

Getting the info field first and then retrieving the coordinates from there is correct, of course. Because this is how you structured your records. You cannot get the coordinates without getting the info first.

What's the value of the info object?

database.getCollection(collectionName).getDocument(documentId).getField("info")

Did you print this to the console, as suggested?

System.out.println(database.getCollection(collectionName).getDocument(documentId).getField("info"));
cristianhoyos66-zz commented 8 years ago

Yes, I printed that, and it shown this: {coordinates: [12345, -12345], name: 'coordinateName'}

ocram commented 8 years ago

That's great! The line that was printed was what we expected, right?

As a next step, can you please check what's the type of infoField? (That's what you printed, isn't it?) That would allow us to extract the single coordinates from the object. It should be an instance of Map<String, Object>, probably of Fields. So let's see what we need:

// check the class of the `infoField` object and print it
System.out.println(infoField.getClass().getName());
// try to convert the object to a map (`{}` when printed) so that we can access the properties
Map<String, Object> infoMap = (Map<String, Object>) infoField;
// try to access the `coordinates` property
Object coordinatesField = infoMap.get("coordinates");

// check the class of the `coordinatesField` object and print it
System.out.println(coordinatesField.getClass().getName());
// try to convert the object to a list (`[]` when printed) so that we can access the elements
List<Object> coordinatesArray = (List<Object>) coordinatesField;

// the single coordinates are now, hopefully, the first two elements
System.out.println(coordinatesArray.get(0)+" and "+coordinatesArray.get(1));

Does that work? What's printed to the console?

ocram commented 8 years ago

Closing due to inactivity

cristianhoyos66-zz commented 8 years ago

Yes, I forgot to close this issue. Thank you anyway, you solved my doubts

ocram commented 8 years ago

No problem :) Thanks!