Json framing error - android

I have to frame a json like the following while making a json request:
{
"method": "version",
"data": {
"username": "korea",
"order_point_of_contact": {
"shipment_version": "1",
"testimonial_version": "3",
"point_of_contact_version": "2"
}
}
}
I use the following code to frame it:
JSONObject data = new JSONObject();
JSONObject pointofContact = new JSONObject();
JSONObject jsonObject = new JSONObject();
try {
data.put("username", "korea");
jsonObject.put("method", "version");
jsonObject.put("data", data);
jsonObject.put("order_point_of_contact", pointofContact);
jsonObject.put("shipment_version", "1");
jsonObject.put("testimonial_version", "3");
jsonObject.put("point_of_contact_version", "2");
} catch (JSONException e) {
e.printStackTrace();
}
But the problem is that it doesn't frame the desired json. What is the error that I'm committing?

You're on the right track, but looks like you're putting the values for shipment_version, testimonial_version and point_of_contact_version on the wrong JSONObject.
Judging by your desired JSON posted above, they should be added to pointofContact.
Your code should be doing something like this:
pointOfContact.put("shipment_version", "1");
pointOfContact.put("testimonial_version", "3");
pointOfContact.put("point_of_contact_version", "2");
data.put("username", "korea");
data.put("order_point_of_contact", pointofContact);
jsonObject.put("method", "version");
jsonObject.put("data", data);
The JSON should be nested correctly then.

Try the below given code:
JSONObject data = new JSONObject();
JSONObject pointofContact = new JSONObject();
JSONObject jsonObject = new JSONObject();
try {
pointofContact .put("shipment_version", "1");
pointofContact .put("testimonial_version", "3");
pointofContact .put("point_of_contact_version", "2");
data.put("username", "korea");
data .put("order_point_of_contact", pointofContact);
jsonObject.put("method", "version");
jsonObject.put("data", data);
} catch (JSONException e) {
e.printStackTrace();
}

Your json creation should be like this
JSONObject data = new JSONObject();
JSONObject pointofContact = new JSONObject();
JSONObject jsonObject = new JSONObject();
jsonObject .put("method", "version");
try {
data.put("username", "korea");
pointofContact.put("shipment_version", "1");
pointofContact.put("testimonial_version", "3");
pointofContact.put("point_of_contact_version", "2");
data.put("order_point_of_contact", pointofContact);
jsonObject .put("data",data);
} catch (JSONException e) {
e.printStackTrace();
}

Related

How to include hierarchy in JSON for Android?

I have this JSON file that I would like to send to a server with POST
{
"header1" : {
"message" : {
"content" : "Hello",
"type" : "text"
},
"header2" : {
"address" : "sample#example.com"
}
}
}
This is the code I have for the json pbject
JSONObject jsonObject = new JSONObject();
jsonObject.put("content", "hello");
jsonObject.put("type", "text");
jsonObject.put("address", "sample#example.com");
String message = jsonObject.toString();
My question is how do I code the hierarchy: header1, message and header2?
JSONObject json = new JSONObject();
JSONObject messageObject = new JSONObject();
JSONObject header2Object = new JSONObject();
try {
messageObject .put("content", "Hello");
messageObject .put("type", "text");
header2Object .put("address", "sample#example.com");
json.put("header1", messageObject.tostring);
json.put("header2", header2Object.tostring );
} catch (Exception ignored) {
}
Try This
I think you should do this in a bottom up fashion.
What I mean is first make a JSONObject with the name header2 and put address in it.
Then make the other JSONObject named message, populate it using put and then in turn place it inside another JSONObject named header1.
JSONObject header2 = new JSONObject ();
header2.put("address", "your address");
Then,
JSONObject header1 = new JSONObject ();
header1.put("header2", header2);
And so on...

Android build a jsonarray of jsonobjects

I'd like to build a json object which contains, among others, an array of objects like this:
{"ChoisiEvents":[{"Id_evt":25},{"Id_evt":4}],"parasite":3}
I have written some code like :
JSONArray JAChoisiEvents = new JSONArray();
JSONObject objEvent = new JSONObject();
try{
if (cbxTemp.isChecked()){
objEvent.put("Id_evt", 25);
JAChoisiEvents.put(objEvent);
}
if (cbxAutreRaison.isChecked()) {
objEvent.put("Id_evt", 4);
JAChoisiEvents.put(objEvent);
}
} catch (JSONException e) {
e.printStackTrace();
}
//...
JSONObject obj = new JSONObject();
try {
obj.put("parasite", iParasite);
System.out.println("ChoisiEvents : " + JAChoisiEvents.toString());
obj.put("ChoisiEvents", JAChoisiEvents); //
} catch (JSONException e) {
e.printStackTrace();
}
I got the following result:
{"ChoisiEvents":[{"Id_evt":4},{"Id_evt":4}],"parasite":3}
As you can see, the last item in my array is repeated each time !
You are using the same JSONObject, thats why value is overriding,
try to instantiate for the second time
objEvent = new JSONObject();
After adding to the first value.
Like this
JSONObject objEvent;
if (cbxTemp.isChecked()){
objEvent = new JSONObject();
objEvent.put("Id_evt", 25);
JAChoisiEvents.put(objEvent);
}
if (cbxAutreRaison.isChecked()) {
objEvent = new JSONObject();
objEvent.put("Id_evt", 4);
JAChoisiEvents.put(objEvent);
}

How to send a JSON object(in get method) over Request with Android?

I want to send the following parameter
data={"method": "category", "parameter": {"id":20, "language":"en"}}
to a web service
How can I do this from android for get method?
i tried but did not work.
your JSON is created like this :
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("method", "category");
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("id", 20);
jsonObject1.put("language", "en");
jsonObject.put("parameter",jsonObject1 );
} catch (JSONException e) {
e.printStackTrace();
}
Now you can add this in your request with key as "data"
try this
JSONObject jsonObject = new JSONObject();
jsonObject.put("id",20 );
jsonObject.put("language","en");
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("method", "category");
jsonObject2.put("parameter", jsonObject.toString());

creating nested json string android

I am creating json string but unable to give tag to JSONObject in nested json string.
Here is what I want
"User": [
{
"User1": {
"name": "name1",
"Address": "add1",
"user_detail": {
"Qualification": B.E,
"DOB": 11/2/1990,
}
}
},
{
"User2": {
"name": "name2",
"Address": "add2",
"user_detail": {
"Qualification": B.E,
"DOB": 11/2/1990,
}
}
}
}
]
And I have managed to get until here
{"User":[{"name":"name1","Address":"Add1"}, {"Qualification": "B.E", "DOB":"11/12/1990"}]}
But I am failed to add tag for JSONObject both for USer and user_details
Here is my code
try {
user = new JSONObject();
user.put("name", "name1");
user.put("Address", "B.E");
} catch (JSONException je) {
je.printStackTrace();
}
try {
userObj = new JSONObject();
userObj.put("User1", user);
jsonArray = new JSONArray();
jsonArray.put(user);
} catch (JSONException j) {
j.printStackTrace();
}
}
try {
users = new JSONObject();
users.put("User", jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}
The main thing is I am do not know, how to give tags to JSONObject.
You could just pass the desired JSON String to the JSONObject constructor to do the job. Take a look here
Current String Contain JSONArray of JSONObject's instead of JSONObject as root element. You can create Current Json String in java as:
JSONArray jsonArray = new JSONArray();
// User1 JSONObjects
user = new JSONObject();
user.put("name", "name1");
user.put("Address", "B.E");
user_obj_one = new JSONObject();
user_obj_one.put("User1",user);
//...same for User2...
...
user_obj_two = new JSONObject();
user_obj_two.put("User2",user_two);
//put in final array :
jsonArray.put(user_obj_one);
jsonArray.put(user_obj_two);
//....

How to convert this data into JSon format and POST this data to server?

I have following string,
String Action="CreateUser";
String company="company name";
But how to convert above string same as below json string and pass this string to server
{"action":"CreateUser","user":{"company":{"solutionname":"","createdon":"","companyguid":"","nextinvoicenumber":"1000","companystatus":"Active","companyname":"","solutioncode":"InvoiceASAP"},"createdon":"","userstatus":"Active","companyguid":"","addressguid":"","guid":"","firstname":"Renuka","lastname":"Shah","email":"ren40#vprex.com","username":"","password":"renuka","cellphone":"","homephone":"","officephone":"","officeextension":"","faxnumber":"","phoneid":"1","sourceid":"1","primarycontact":"Y","permissioncode":"COMPANYADMIN","address":{"addr1":"","addr2":"","addr3":"","addr4":"","addr5":"","city":"","state":"","postalcode":"","country":"","note":""}}}
Any help would be appreciated.
Do as following: http://snipplr.com/view/53225/
String Action="CreateUser"; String company="company name";
try
{
JSONObject action=new JSONObject();
JSONObject user=new JSONObject();
action.put("action", Action);
action.put("user", user);
JSONObject company=new JSONObject();
user.put("company", company);
user.put("userstatus", "Active");
company.put("solutionname", "");
company.put("nextinvoicenumber", "1000");
//....
}
catch (Exception je)
{
}
and then pass the JSONObject (action) to the server..
You will have put put values into a JSONObject in the form of name-value pairs.
Like this:
public void writeJSON() {
JSONObject user = new JSONObject();
JSONObject user2;
user2 = new JSONObject();
try {
user.put("dish_id", "1");
user.put("dish_custom", "2");
user.put("quantity", "2");
user.put("shared", "2");
user2.put("dish_id", "2");
user2.put("dish_custom", "2");
user2.put("quantity", "4");
user2.put("shared", "3");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Categories

Resources