how to convert json object to string in android..? - android

I want to convert from JSONObject
{"CNo":80,"CName":"ganesh","CMail":"ganesh#ganesh.com","CMailType":"home","CPhNo":9878987776,"CPhNoType":"home","ClientNo":1}
to
{\"CNo\":80,\"CName\":\"ganesh\",\"CMail\":\"ganesh#ganesh.com\",\"CMailType\":\"home\",\"CPhNo\":9878987776,\"CPhNoType\":\"home\",\"ClientNo\":1}

Try to use toString() on your JSON object

I hope this helps. Just learnt it yesterday :)
JSONObject foo = yourJSONOject;
String uid = foo.get("CNo").isString().toString();
String type = foo.get("CName").isString().toString();
.
. //for each Key field.
I am not sure why you have put the escapes in the string, but you can call append() and get the OP as you want it.

Below code will convert the given JsonObject to string.
Gson gson = new Gson();
String bodyInStringFormat = gson.toJson(passYourJsonObjectHere);

Related

How to send list of byte arrays to json

How i can pass ArrayList<byte[]> to JSONObject as a parameter?
For example, I need to send a Id (string), Name (string) and photoList (ArrayList<byte[]>) in a json object.
You can use GSON LIBRARY . The code you need can be as :
String jso;
jso = new Gson().toJson(photoList);
It should convert the arraylist(i.e. photoList) to jsonObject.
Just use GSONlib for this if you want to convert arraylist into json
If you are using GSON library use the following code to convert your ArrayList to JSON.
String jsonString = new Gson().toJson(yourArrayList);

Android Convert String to JSONObject

I want to convert a string to JSONObject. Below is sample code.
String str = "{"time": 1449838598.0999202, "Label": "Shirt", "Price": 52}";
JSONObject obj = new JSONObject(str);
But after conversion time becomes 1.4498385980999203E9.
Any Help will be appreciated.
Thanks
If you really want it to be formatted like that then make it into a string instead of a number. Just add double quotes around the item. But if your using it for a calculation on the other end, i would leave it as is as you'll have to convert it back to a int anyway. If you want to use gson as an alternative check here: How to prevent Gson from converting a long number (a json string ) to scientific notation format?
Write it like this in double quote. "1449838598.0999202"
Like Abhishek said, write time & price in double quote as "1449838598.0999202" and "52".
Then you can save the string to Gson, and then convert it to Json.
String str = "{"time": "1449838598.0999202", "Label": "Shirt", "Price": "52"}";
Gson gson = new Gson();
String json = gson.toJson(str);

How to extract objects from json

I have a scenario where I need only 1 objects out of the entire json.
{"id":"1","first_name":"Steve","last_name":"Holt","user_type":"Teacher","user_key_area":"Math"}
In above I want to extract user_type.
How will I do?
You can use Google GSON to parse the json response and map it to the model directly . Here is the tutorial for the same TUTORIAL
Use this to extract the user_type from the json, pass the your json response in place of response variable.
JsonObject object =new JsonObject(response);
String user_type = object.getString("user_type");
You are getting this json in response you can get 1 object like this:
String value = response.getString("Key Name");
Get Response and that create JsonObject
JsonObject jsonObject =new JsonObject(res);
Create String object and pass string parameter "user_type"
String userType = jsonObject.getString("user_type");

How to Get JSONObject Only Android

{"StatusID":"1","Error":"Register Successfull."}
This my JSONObject I don't know how to convert it to string in android
Advice me Please
Thank.
Get the JSON object and get values like this.
JSONObject jsonObject = new JSONObject(res);
String statusId = jsonObject.getString("StatusID");
String error = jsonObject.getString("Error");
If you want just convert this json:
{"StatusID":"1","Error":"Register Successfull."}
you must use toString() method.
If you want get "status id" or "error" you should use getString() method
See THIS.

how to generate JsonStringer for this Json Data Format?

hello i have a JSON data format can anyone please help me to make dynamic JSONStringer object for this String
{"Text":"Hello Simple Text",
"Files":[{"ContentType":"image/png",
"Content":"iVBORw0KGgoAAAANSUhEUgAAAR8AAACMCAIAAADKsDpDAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAB3RJTUUH2wYWDzIB3zSYdQAAAAd0RVh0QXV0aG9yAKmuzEgAAAAMdEVYdERlc2NyaXB0aW9uABMJISMAAAAKdEVYdENvcHlyaWdodACsD8w6AAAADnRFWHRDcmVhdGlvbiB0aW1lADX3DwkAAAAJdEVYdFNvZnR3YXJlAF1w/zoAAAALdEVYdERpc2NsYWltZXIAt8C0jwAAAAh0RVh0V2FybmluZwDAG+aHAAAAB3RFWHRTb3VyY2UA9f+D6wAAAAh0RVh0Q29tbWVudAD2zJa/AAAABnRFWHRUaXRsZQCo7tInAAABAElEQVR4nO2de1zUVf7/3+dzmwsMoCgDXgARBO/"}],
"AuthToken":"XkWQRd65+H+iPtlOoAEYAR0jrzB1o3UV"}
i have used
jsonstr = new JSONStringer().object().key("Text")
.value(msg).key("Files").array().object().key(
"ContentType").value("image/png").key(
"Content").value(enimg)
.endObject().endArray().key("AuthToken").value(token)
.endObject();
but the server is giving me fault message in return, not accepting the data.
actually i was doing the right thing..everything was OK..
the problem was with org.json package it was not accurate with Base64 string
i switched to another library and all worked..
https://stackoverflow.com/questions/338586/a-better-java-json-library
see the above question for another json libraries
that was problem with org.json
i switched to another..and all it works
nesting too deep in JSON... should I switch to XML?
This is a way to do what you want:
// Creating root JSONObject
JSONObject json = new JSONObject();
// Put in it a String field
json.put("Text", "Hello sample");
// Creating a JSONArray
JSONArray arr = new JSONArray();
//Creating the element to populate the array
JSONObject element = new JSONObject();
element.put("ContentType","image/png");
element.put("Content","iVBORw0K...gDXgARBO/");
// Put it in the array
arr.put(element);
// Put the array and other fileds in the root JSONObject
json.put("Files", arr);
json.put("AuthToken", "XkWQ...o3UV");
// Get the JSON String
String s = json.toString();
// Get formatted and indented JSON String
String s2 = json.toString(4);
// 4 is the number of spaces to indent the string
You can use JSONObject class for this purpose
http://developer.android.com/reference/org/json/JSONObject.html

Categories

Resources