Android JSONObject


Android JSONObject Tutorial and Examples

JSONObject is a class representing a modifiable set of name/value mappings.

For these mapping names have to be unique and non-null strings.

On the hand values may be any mix of :

  1. JSONObjects
  2. JSONArrays
  3. Strings
  4. Booleans
  5. Integers
  6. Longs
  7. Doubles or
  8. NULL.

Values may not be null, NaNs, infinities, or of any type not listed above.

Take note that NULL is different from null. Continue reading to check difference.

NULL vs null

NULL is an Object representing a sentinel value used to explicitly define a name with no value.

The difference with null is that values with NULL:

  1. show up in the names() array.
  2. show up in the keys() iterator
  3. return true for has(String)
  4. do not throw on get(String)
  5. are included in the encoded JSON string.

This value violates the general contract of equals(Object) by returning true when compared to null. Its toString() method returns null.

Quick JSONObject Examples

Let's look at some quick JSON examples.

1. How to Generate JSON using JSONObject

JSONObject remember, we said is a class representing a modifiable set of name/value mappings.

Let's say we want to create a class responsing for creating us JSON data.

First step is to import the classes we want to use:

import org.json.JSONException;
import org.json.JSONObject;

In this case we've imported JSONObject and JSONException, both from the org.json package.

Then as an instance field we instantiate the JSONObject:

    private JSONObject object = new JSONObject();

Then creat a method that will receive a name String and a value object and put them in our JSONObject instance.

To put them we simply use the put() method. Then we you invoke the public gen() of the class instance we simply return you our JSONObject instance.


import org.json.JSONException;
import org.json.JSONObject;

public class JsonGenerator {

    private JSONObject object = new JSONObject();

    public JsonGenerator put(String name, Object value){
        JSONObject tmpObj = object;
        try {
            object.put(name,value);
        } catch (JSONException e) {
            object = tmpObj;
        }
        return this;
    }

    public JSONObject gen() {
        return object;
    }
}
2. JSONObject Example

Here's another JSONObject example

import org.json.JSONObject;

public class MrHelloWorld { 

    public static void main(String[] args) {
        // convert Java to json 
        JSONObject root = new JSONObject();
        root.put("message", "Hello");
        JSONObject place = new JSONObject();
        place.put("name", "World!");
        root.put("place", place);
        String json = root.toString();
        System.out.println(json);

        System.out.println();
        // convert json to Java 
        JSONObject jsonObject = new JSONObject(json);
        String message = jsonObject.getString("message");
        String name = jsonObject.getJSONObject("place").getString("name");
        System.out.println(message + " " + name);
    } 
} 

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post