wolverine
wolverine

Reputation: 1665

How to Create JSON data with some complexity?

I want to create json data of format like this:

{
   "Credential":{
      "ref1":"Test",
      "ref2":"test",
      "ref3":"test"
   },
   "ref4":"data"
}

I tried a lot, but i did not find a way to do this. Can anyone please help me. EDIT:

I am able to put the data like below:

   {
      "ref1":"Test",
      "ref2":"test",
      "ref3":"test"
   }

Thanks in advance.

Upvotes: 4

Views: 1863

Answers (2)

MKJParekh
MKJParekh

Reputation: 34301

You can always use JSONStringer class to do so,

JSONStringer jsonstr  = new JSONStringer()
                           .object().key("Credential")
                                    .object().key("ref1").value("Test")
                                             .key("ref2").value("test")
                                             .key("ref3").value("test")
                                    .endObject()
                                    .key("ref4").value("data")
                           .endObject();
Log.i("JSONStringer", jsonstr.toString());

Upvotes: 6

Conscious
Conscious

Reputation: 1663

why do not you use google gson?

// get Json string
Gson gson = new Gson();
String strJson = gson.toJson(yourObject);

Is not it a better solution?

Upvotes: 0

Related Questions