-2

I want to get a list of friends' names from the Facebook API in Android. In that process, I want to learn how to read JSON objects/Arrays.

I have JSONObject and/or JSONArrays that have been passed to me. I don't know what's in them*. I know how to read the data once I know what fields exist, but I can't read anything as far as I can tell, without a key. Even with the key, How can I tell what's in it?

Basically, I'd like a piece of code that looks like:

JSONArray mArray = response.getJSONArray();
String theEntireDatabase = mArray.getStringOFEntireDatabase();

and have it respond with a String that looks like this:

{
   "phoneNumber": [
      {
         "type": "work",
         "num": "11111"
      },
      {
         "type": "home",
         "num": "2222"
      }
   ],
   "address": {
      "state": "World",
      "address": "infinite space, 000",
      "city": "Android city"
   },
   "surname": "Swa",
   "name": "Android"
}

Having known nothing of what is in the database beforehand?

*it could be "color:" it could be "nuclear threat level:" for all I know.

I tried this, but it only gives keys: Javascript get JSON key Name

Community
  • 1
  • 1
Glen Pierce
  • 4,401
  • 5
  • 31
  • 50

4 Answers4

1

JSONObject#keys will tell you what keys are defined for the object. JSONArray#length will tell you how many entries are in the array (and therefore the range of values you can use for index with the other methods: 0 through length() - 1). JSONObject#toString/JSONArray#toString will give you the string you've asked for.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

Take a look at Jacksons built-in tree model feature. http://wiki.fasterxml.com/JacksonTreeModel

And your code will be:

public void parse(String json)  {
       JsonFactory factory = new JsonFactory();

       ObjectMapper mapper = new ObjectMapper(factory);
       JsonNode rootNode = mapper.readTree(json);  

       Iterator<Map.Entry<String,JsonNode>> fieldsIterator = rootNode.fields();
       while (fieldsIterator.hasNext()) {

           Map.Entry<String,JsonNode> field = fieldsIterator.next();
           System.out.println("Key: " + field.getKey() + "\tValue:" + field.getValue());
       }
}
Archit Garg
  • 1,012
  • 8
  • 16
1
JSONArray mArray = response.getJSONArray();
String theEntireDatabase = mArray.toString();

This should work well.
If you want the JSONArray pretty printed you can add this after the code I've just provided you.

Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(theEntireDatabase);
String prettyJsonString = gson.toJson(je); 
arodriguezdonaire
  • 5,396
  • 1
  • 26
  • 50
0

besides the technical answers (JSONObject#toString(n) seems the easiest - see http://developer.android.com/reference/org/json/JSONObject.html),

I would often use two more pragmatic solutions:

  1. look for an API documentation... I know, it sounds crazy, but some companies do offer such a thing
  2. hack together a short javascript script to try the API and inspect the result in the developer console/firebug/whatever is handy
cypherabe
  • 2,562
  • 1
  • 20
  • 35