How do I remove these "(commas) of the JsonObject? - android

This is my JsonObject
{"Table":"[{\"FailureIds\":\"\",\"SuccessIds\":\"1167, 8789, 10764, 11935, 937, 938, 939, 940, 3980, 3981, 3982, 3983, 3984, 3985, 3986, 3987, 3988, 3989, 3990, 3991, 6045, 6046, 6047, 6048, 6049, 6050, 6051, 7453, 7454, 7455, 7456, 7457, 8559, 8560, 8561, 8562, 10432, 10433, 10434, 10435, 10436, 10437, 10438, 11705, 11706, 11707, 11708\"}]"}
I need to get rid of the starting " and ending " from [{ and after }]
I have already tried with JsonElement as below with no success.
String temp="["+successIds.toString()+"]";
JsonObject jsonObjec=new JsonObject();
Gson gson3 = new Gson();
JsonElement jsonElement = gson3.toJsonTree(temp);
jsonObjec.add("Table",jsonElement);
The successIDs was a JSONobject containing failureIDs and SuccessIDs which I converted to String.

Refer the code snippet below to create your JSON using Google's Gson library without extra quotes.
Gson gson = new Gson(); //Creates Gson Object
JsonObject jsonObject = new JsonObject();
jsonObject.add("FailureIds", gson.toJsonTree(FailureIds)); //Adding array of failure ids to inner object
jsonObject.add("SuccessIds", gson.toJsonTree(SuccessIds)); //Adding array of success ids to inner object
JsonObject finalObject = new JsonObject();
finalObject.add("Table", gson.toJsonTree(jsonObject));
String jsonStr = gson.toJson(finalObject); //Converts final object to valid Json string
Final Json will look like
{"Table": {"FailureIds":[1,2,5], "SuccessIds":[4,6,7]}}

Related

JSON object building

Currently I have this JSON structure:
{"title1":["info1","info2"],"title2":"info2","title3":"info3"}
What I need to achieve is this:
{"title4":[{"a":"aa","b": "bb"}]}
To achieve the above, I use JSONArray:
JSONArray list = new JSONArray();
list.put("info1");
list.put("info2");
obj.put("title1", list);
And JSONObject:
JSONObject obj = new JSONObject();
obj.put("title2", "info2");
How can I visualize the desired output?
To achieve what you want, a JSONObject with a JSONArray containing JSONObjects, you can use the following code
JSONObject root = new JSONObject(); // The root JSON object
JSONArray title4 = new JSONArray(); // The JSON array that will contain JSON objects
// The JSON objects
JSONObject a = new JSONObject();
a.put("a", "aa");
JSONObject b = new JSONObject();
b.put("b", "bb");
title4.put(a);
title4.put(b);
// Put the JSON array in the root JSON object
root.put("title4", title4);

How to post data in my json format

I am new in android and confuse in post data in json format using volley lib.
My Json param like:
{
key
comKey
addLeadArray
[{
key
autoid
images[image1,image2...]
audio
}
{
key
autoid
images[image1,image2...]
audio
}
{
key
autoid
images[image1,image2...]
audio
}.....]
}
I am trying code:
JSONObject object = new JSONObject();
object.put("key", "1");
object.put("comKey", "2");
................
JSONObject addLeadArrayObj= new JSONObject();
AddLeadArray.put("key", "1");
AddLeadArray.put("autoid", "34");
................
object.put("addLeadArray", addLeadArrayObj);
But its create {{}} and I want to make json object for above json formate. what can I do please help me and pls give code snippt.
What you are doing is creating JSONObject AddLeadArray and putting it in the main JsonObject.
AddLeadArray is an array, so make it an object of JSONArray instead of JSONObject.
JSONObject object = new JSONObject();
object.put("key", "1");
object.put("comKey", "2");
................
// Create JSONArray
JSONArray addLeadArrayObj= new JSONArray();
// Create JSONObject for jsonArray
JSONObject object2 = new JSONObject();
object2.put("key", "1");
object2.put("autoid", "34");
// put object2 in addLeadArray
addLeadArrayObj.put(object2);
// put addLeadArray in main jsonobject
object.put("addLeadArray", addLeadArrayObj);
Refer more here: Add JsonArray to JsonObject
How about using Gson.
Gson gson = new Gson(); // Or use new GsonBuilder().create();
MyType target = new MyType();
String json = gson.toJson(target); // serializes target to Json
MyType target2 = gson.fromJson(json, MyType.class); // deserializes json into target2
If the object that your are serializing/deserializing is a ParameterizedType (i.e. contains at least one type parameter and may be an array) then you must use the toJson(Object, Type) or fromJson(String, Type) method.
Here is an example for serializing and deserialing a ParameterizedType:
Type listType = new TypeToken<List<String>>() {}.getType();
List<String> target = new LinkedList<String>();
target.add("blah");
Gson gson = new Gson();
String json = gson.toJson(target, listType);
List<String> target2 = gson.fromJson(json, listType);
Here is the Gson Help.

(Android) JSONObject cannot be converted to JSONArray

My below android code is throwing
org.json.JSONException: Value
{"ID":1,"DisplayName":"Manish","UserName":"manish.parab#hotmail.com"}
at AuthenticateUserResult of type org.json.JSONObject cannot be
converted to JSONArray
Code:
String response = Common.ExecuteHttpRequest(Url);
JSONObject jsonObject = new JSONObject(response);
JSONArray jArray = jsonObject.getJSONArray("AuthenticateUserResult");
response is string from WCF method.
{"AuthenticateUserResult":{"DisplayName":"Manish","ID":1,"UserName":"manish.parab#hotmail.com"}}
The value of AuthenticateUserResult is a JSON Object (it's enclosed in {}).
Change that line to this
JSONObject jArray = jsonObject.getJSONObject("AuthenticateUserResult");
Then you can get your data as follows :
String displayName = jArray.getString("DisplayName");
// Etc...
There are three workarounds to solve this problem.
1.Use JsonObject. Your WCF server just give it in JsonObject.
String response = Common.ExecuteHttpRequest(Url);
JSONObject jsonObject = new JSONObject(response).getJSONObject("AuthenticateUserResult");
2.Use json array as a container
String response = Common.ExecuteHttpRequest(Url);
JSONObject jsonObject = new JSONObject(response);
JSONArray jArray = new JSONArray().put(jsonObject.getJSONObject("AuthenticateUserResult"));
3.Edit server to provide AuthenticationUserResult into json array. The right format would be as below.
{"AuthenticateUserResult":[{"DisplayName":"Manish","ID":1,"UserName":"manish.parab#hotmail.com"}]}
The exception is right, because "AuthenticateUserResult" value is declared as an element ({}) and not as an array ({}).
To fix this, use getJSONObject method to get the value of "AuthenticateUserResult", like this:
String response = Common.ExecuteHttpRequest(Url);
JSONObject jsonObject = new JSONObject(response);
JSONObject result = jsonObject.getJSONObject("AuthenticateUserResult");
After that, you can retrieve a child element, like:
String mUserName = result.getString("UserName");

Android parse JSON, how to fetch only single parameter

hi i have only one parameter to parse
this is what i have tried....
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray("result");
and here is the below json
{"result" : "169489465811879423"}
Use this
JSONObject jsonObject = new JSONObject(response);
String result = jsonObject.getString("result");
Here there is no JsonArray and you are trying to get an array

Parsing json (2 arrays) in Android

I need to parse a JSON with two arrays.
resp: {"attending": [], "people": []}
I tried this
JSONObject AttendingArray = new JSONObject(resp);
JSONArray ParkArray = new JSONArray("people");
JSONArray AttendingArray = new JSONArray("attending");
But it doesn't work
12-01 22:47:53.074: WARN/System.err(30814): org.json.JSONException: Value people of type java.lang.String cannot be converted to JSONArray
Thanks!
JSONObject obj = new JSONObject(resp);
JSONArray ParkArray = new JSONArray(obj.getString("people"));
JSONArray AttendingArray = new JSONArray(obj.getString("attending"));
You must pass actual JSON to the JSONArray constructor, not just the name of the json array - the new JSONArray constructor call doesn't know about the response, so you need to give it some data, not just "people"

Categories

Resources