How to Get JSONObject Only Android - 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.

Related

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 values from JSON

I have some data in JSON format like this:
{"Datos":[{"cli_nombre":"Futcho 7","suc_nombre":"Naucalpan"}],"status":0,"mensaje":""}
The status and mensaje keys are not in Datos array. How can I get it?
I can get the values to the unique Object, this object has two keys cli_nombre and suc_nombre and I don't have problems to get the values for this keys.
Regards
If you have that JSON in a String, you could use the following code to get the status and mensaje:
JSONObject jsonObject = new JSONObject(yourJSONString);
int status = jsonObject.getInt("status");
String mesaje = jsonObject.getString("mensaje");
Hope it helps you! :)
It's not in the array but in the JSONObject.
So simply call
jObject.getInt("status");

How is this JSON string supposed to be parsed to get the different fields

I have this JSON string that I am recieving after I make a purchase from the playstore and I am apparently missing something in my code when I am trying to parse it.
PurchaseInfo:{"orderId":"12999763165555505758.1317333586444405",
"packageName":"com.mypkgname.myapp",
"productId":"monthly_purchase_01",
"purchaseTime":1357456489000,
"purchaseState":0,
"purchaseToken":"yrynypfkdncvhlxdbypysvwz.AO-J1OxFkndfqkClAqbbYAOApkMgTG4VX9Ef0uNP0FIs9-xGrXivkbx3FNMA2yNU12K_sbvRGFcknVBTfisI-uZawCXLGlMX4v4Zw8GFOmS0Q6PIbiITTGqn5h1QbEB4Rv84sXdUJHP3B_UQfujZN7ADi9bm_N4_iA"}
Here is the snippet of code I am using it to attempt the parsing
try {
JSONObject j1 = new JSONObject(tester1);
JSONArray mPurchInfo = j1.getJSONArray("PurchaseInfo");
int count = mPurchInfo.length();
final String[] purchInfo = new String[count];
JSONObject q1 = mPurchInfo.getJSONObject(0);
purchInfo[0] = q1.getString("orderId");
purchInfo[1] = q1.getString("packageName");
purchInfo[2] = q1.getString("productId");
purchInfo[3] = q1.getString("purchaseTime");
purchInfo[4] = q1.getString("purchaseState");
purchInfo[5] = q1.getString("purchaseToken");
orderID=purchInfo[0];
} catch (JSONException e) {
Log.e("Error","Yes");
}
I am catching the an error as I see this last log statement in my log but I am still trying to learn the parsing JSON Strings
I hope I am at least close
Ideally I would like to have Strings set to all the values in the JSON String
orderID = ??
packageName = ??
etc.
Thanks
I have this JSON string that I am recieving after I make a purchase from the playstore
If that's really what you get back, you need to contact them and tell them to fix their API; that string is not valid JSON. There are two problems with it:
A JSON document must have an object or array as the top-level item. The string as quoted is missing a { at the beginning and a } at the end.
All keys in JSON must be in double quotes. The first one, PurchaseInfo, is not.
Also, your code is doing this:
JSONArray mPurchInfo = j1.getJSONArray("PurchaseInfo");
...but if it were valid JSON, PurchaseInfo wouldn't be an array, it'd be an object.
Looking at it, if you remove the PurchaseInfo: at the beginning, it's valid. Once you've removed that, this line:
JSONObject j1 = new JSONObject(tester1);
...will give you an object from which you can query information:
String orderId = j1.getString("orderId");

Issue initializing a JSONObject

I'm trying to initialize a JSONObject with the following string, received from a web service:
"{
"campaignid": "8",
"campaignname": "Pilotarienak 2011",
"campaignlink": "http:\\/\\/www.xxx.com\\/fr\\/cote-basque\\/agenda\\/2011-05-20\\/FMAAQU064FS016DV-pilotarienak-d-anglet?fromapp",
"splash": "http:\\/\\/www.xxx.com\\/ads\\/customers\\/pilotarienak\\/320x480.jpg",
"banner": "http:\\/\\/www.xxx.com\\/ads\\/customers\\/pilotarienak\\/320x160.jpg"
}"
It seems to be valid json (it validates in jsonlint.com), but when initializing a JSONObject with that I get:
org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
Anybody can help?
Thanks
Seems like you are trying to instantiate it from a String with extra quotes. You need to remove the wrapping quotes(I'm not using your string, but giving an example to make it clearer):
This is OK:
String jStr= "{\"param1\":\"hello\"}";
JSONObject jObj = new JSONObject(jStr);
This is not:
String jStr= "\"{\"param1\":\"hello\"}\"";
// note this ^^ and this ^^
JSONObject jObj = new JSONObject(jStr);
Try to rewrite all, in a simplified mode (just for test). I think that you put some invalid character.
Try to remove All the "\" caracters

how to convert json object to string in 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);

Categories

Resources