2

I am confused as to what a JSON Object is and what a JSON String is. Which part is a JSON Object, and which is a JSON String?

JSON example 1:

{ 
    "abc":"v1",
    "def":"v2"
}

JSON example 2:

{
    "res":"false",
    "error":{
        "code":101
    }
}
Knossos
  • 15,802
  • 10
  • 54
  • 91
chandan
  • 19
  • 6

5 Answers5

0

Data is represented in name/value pairs.

"abc":"v1"

Curly braces hold objects and each name is followed by ':'(colon), the name/value pairs are separated by , (comma).

{ 
"abc":"v1",
"def":"v2"
}

Code Example:

JSONObject obj = new JSONObject(jsonStr);
String abc = obj.get("abc");

Square brackets hold arrays and values are separated by ,(comma).

{
   "books": [

      {
         "id":"01",
         "language": "Java",
         "edition": "third",
         "author": "Herbert Schildt",
      },

      {
         "id":"07",
         "language": "C++",
         "edition": "second",
         "author": "E.Balagurusamy",
      }

   ]
}

Code Example:

JSONArray arrBooks = new JSONArray("books");
for (int i = 0; i<=arrBooks.length(); i++){
      JSONObject objBook = arrBooks.getJSONObject(i);
      String id = c.getString("id");
}
Myth
  • 1,218
  • 12
  • 15
0

Given by your first example:

try {
    JSONObject obj = new JSONObject(json);
    String abc = obj.get("abc");
    String def = obj.get("def");
} catch (Throwable t) {
    // Log something maybe?
}
Carnal
  • 21,744
  • 6
  • 60
  • 75
0

Simply create a JSONObject with that string in the constructor.

JSONObject obj = new JSONObject(your_string_goes_here);

Your JSON string is the entire visual representation that you see (encoded as a string):

{ 
    "abc":"v1",
    "def":"v2"
}

You can tell where a specific JSON Object starts and ends within your string, by looking for that opening brace { and the closing brace '}'.

In your examples, this is a JSON Object:

{ 
    "abc":"v1",
    "def":"v2"
}

So is this:

{
    "res":"false",
    "error": {
        "code":101
    }
}

And this:

{
    "code":101
}
Knossos
  • 15,802
  • 10
  • 54
  • 91
0

Use GSON for parsing & below are the model classes for json1 & json2

public class Json1 {


    /**
     * abc : v1
     * def : v2
     */

    private String abc;
    private String def;

    public String getAbc() {
        return abc;
    }

    public void setAbc(String abc) {
        this.abc = abc;
    }

    public String getDef() {
        return def;
    }

    public void setDef(String def) {
        this.def = def;
    }
}

Json2

public class Json2 {

    /**
     * res : false
     * error : {"code":101}
     */

    private String res;
    /**
     * code : 101
     */

    private ErrorBean error;

    public String getRes() {
        return res;
    }

    public void setRes(String res) {
        this.res = res;
    }

    public ErrorBean getError() {
        return error;
    }

    public void setError(ErrorBean error) {
        this.error = error;
    }

    public static class ErrorBean {
        private int code;

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }
    }
}

I have used GsonFormatter plugin for creating model classes, Use Gson, It is super easy and you dont need to parse anything

Amey Jahagirdar
  • 455
  • 1
  • 4
  • 14
0

JSON comprises JSONObject & JSONArray. Json-1 is JSONObject while JSON -2 is also JSONObject which contains another JSONObject with a key "error".JSON String is the string representation of JSONObject which you can get by JSONObject jsonObject = new JSONObject(); String jsonString = jsonObject.toString();

Aman Srivastava
  • 1,007
  • 1
  • 13
  • 25