-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

4 Answers 4

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); 
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

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

1 Comment

The developer reference had been a bit frustrating, but finding the .toString() method was the "key".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.