How to convert JSONObject to JSONArray in Android? - android

I have request something like this.
{"REQ_DATA":
{"CLPH_NO":"010123456789","USE_INTT_NO":""}
}
but server accepts only this
{"REQ_DATA":
[{"CLPH_NO":"010123456789","USE_INTT_NO":""}]
}
What should I do? I quite noob about JSON, please help me.

REQ_DATA needs to be a JSONArray. Try implementing something like this
try {
JSONObject object = new JSONObject();
JSONArray requiredDataArray = new JSONArray();
JSONObject data = new JSONObject();
data.put("CLPH_NO", "010123456789");
data.put("USE_INTT_NO", "");
requiredDataArray.put(data);
object.put("REQ_DATA", requiredDataArray);
Log.d("JSON", object.toString());
} catch (JSONException e) {
e.printStackTrace();
}

You can try:
JSONObject fromRequest = request.getJSONObject();// given from request
JSONObject toServer = new JSONObject();
JSONArray arr = new JSONArray();
arr.put(fromRequest.get("REQ_DATA"));
toServer.put("REQ_DATA", arr);

Jsonarray array = new Jsonarray();
Jsonobject insidearray = new jsonobject;
insidearray.put("CLPH_NO", "010123456789");
insidearray.put("USE_INTT_NO", "");
array.put(insidearray);
Jsonobject object = new Jsonobject();
object.put("REQ_DATA",array);
The variables may need a little editing, doing this from a phone but there you go

You don't change a JSONObject to a JSONArray, rather, you create a JSONArray and then add the JSONObject to that array.
try {
JSONObject reqData = new JSONObject();
reqData.put("CLPH_NO", "010123456789");
reqData.put("USE_INTT_NO", "");
JSONArray array = new JSONArray();
array.put(reqData);
JSONObject request = new JSONObject();
request.put("REQ_DATA", reqData);
String requestAsJSONString = request.toString();
// call web service
} catch (JSONException e) {
// handle exception
}
You need to send requestAsJSONString to the server.
Furthermore, I suggest you put the JSON object keys in final fields, like so:
static final String KEY_REQ_DATA = "REQ_DATA";
and then use KEY_REQ_DATA in your code instead of using the hardcoded String.

This is simple solution for your json string as per server check it
JSONObject jo = new JSONObject();
try {
jo.put("CLPH_NO", "010123456789");
jo.put("USE_INTT_NO", "");
} catch (Exception e) {
}
JSONArray ja = new JSONArray();
ja.put(jo);
JSONObject final_jo = new JSONObject();
try {
final_jo.put("REQ_DATA", ja);
} catch (Exception e) {
}
Toast.makeText(getApplicationContext(),final_jo.toString(),Toast.LENGTH_LONG).show();

Just simple solution without using any hard-coded Strings
JSONObject currentJson = new JSONObject(yourJsonString); //Your current jsonObject
JSONObject newJsonObject = new JSONObject(); //new jsonObject you want to send to server
newJsonObject.put(currentJson.keys().next().toString(),
new JSONArray().put(currentJson.getString(currentJson.keys().next().toString())));

Related

Read Json in android

{"721xxxxxxx":true,"722xxxxxxx":true,"723xxxxxxx":true}
How can i read all the keys in the json and how can count all the data in the Jsonarray
My code-->
String members = s;
/* s contain {"721xxxxxxx":true,"722xxxxxxx":true,"723xxxxxxx":true} */
JSONArray jsonChildArr = new JSONArray(members);
Log.e("Error","Data in jsonArray--->"+jsonChildArr.length());
Simple Way to Read JSON Object
JSONObject jsonObject = new JSONObject(members);
Log.i("Data","Data in jsonObject--->"+jsonObject.getBoolean("721xxxxxxx"));
Do Like this.
try {
String members = s;
/* s contain {"721xxxxxxx":true,"722xxxxxxx":true,"723xxxxxxx":true} */
JSONObject obj = new JSONObject(members);
JSONArray array = obj.names();
Log.d("ItemCount",array.length()+"");
for(int i=0;i<array.length();i++){
Log.d("Item",array.getString(i)+":"+ obj.getBoolean(array.getString(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
Try this,
JSONObject jsonObject = new JSONObject(result);
boolean data = jsonObject .getBoolean("721xxxxxxx");
Log.d("Data","data:"+data );
try this:
String jsonStr = "{"721xxxxxxx":true,"722xxxxxxx":true,"723xxxxxxx":true}";
JSONObject itemJsonObj = new JSONObject(jsonStr );
HashMap<String,Boolean> result = new HashMap<>();
while(itemJsonObj.keys().hasNext()){
//parse json and save as key-value.
result.put(itemJsonObj.keys().next()+"",itemJsonObj.get(itemJsonObj.keys().next());
// result.size() is like array's size.
}
I wrote a library for parsing and generating JSON in Android http://github.com/amirdew/JSON
for your sample:
JSON jsonData = new JSON(jsonString);
int count = jsonData.count();

Android HTTP Post- send Json Parameters without double quotes

I want to send Json Parameters (below) in Android - POST Method.
{"message":"This is venkatesh","visit":[5,1,2]}
I tried the below code
String IDs="5,1,2";
JSONObject jsonObject = new JSONObject();
jsonObject.put("message", "This is venkatesh");
JSONArray jsonArray = new JSONArray();
jsonArray.put(IDs);
jsonObject.put("visit", jsonArray);
String json = jsonObject.toString();
Log.d("Mainactivity", " json" + json);
I am getting the output is
{"message":"This is venkatesh","visit":["5,1,2"]}
// Output i am get with double quotes inside visit
{"message":"This is venkatesh","visit":[5,1,2]}
// I want to send this parameter without Double quotes inside the Visit
String IDs="5,1,2";
String[] numbers = IDs.split(",");
JSONArray jsonArray = new JSONArray();
for(int i = 0; i < numbers.length(); i++)
{
jsonArray.put(Integer.parseInt(numbers[i]));
}
Hope this helps.
In array add it as integer not as a String
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("message", "This is venkatesh");
JSONArray jsonArray = new JSONArray();
jsonArray.put(5);
jsonArray.put(1);
jsonArray.put(2);
jsonObject.put("visit", jsonArray);
String json = jsonObject.toString();
Log.i("TAG", " json" + json); //{"message":"This is venkatesh","visit":[5,1,2]}
} catch (JSONException e) {
e.printStackTrace();
}
Just replace following line:
jsonArray.put(IDs);
with following code:
jsonArray.put(5);
jsonArray.put(1);
jsonArray.put(2);
So you should use 'int' values if you want to see array without quotes. The point is 'quotes' means that this is String object. Proof is following line of your code:
String IDs="5,1,2";
JSONObject jsonObject = new JSONObject();
jsonObject.put("message", "This is venkatesh");
JSONArray jsonArray = new JSONArray(new int[](5, 1, 2));
jsonObject.put("visit", jsonArray);
I am assuming you will convert the String to integer array and after you do this is how you can add,
Well the only difference you need to understand is that, JSON adds double quotes for values of String and not of Integer.
so for Key value pair for String it would be
"key":"value"
so for Key value pair for Integer it would be
"key":123
so for Key value pair for boolean it would be
"key":true
With that knowledge you can edit your code.
Code
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("message", "This is venkatesh");
JSONArray jsonArray = new JSONArray();
jsonArray.put(0,5);
jsonArray.put(1,1);
jsonArray.put(2,2);
jsonObject.put("visit", jsonArray);
Log.d("TAG","result "+jsonObject.toString());
} catch (Exception e) {
e.printStackTrace();
}
Output
{"message":"This is venkatesh","visit":[5,1,2]}
int[] arrayOfInteger=[1,2,3];
JSONObject jsonObject =new JSONObject();
jsonObject .put("message","your message");
JSONArray jsonArray = new JSONArray(arrayOfInteger);
jsonObject .put("visit",jsonArray );
Result : {"message":"your message","visit":[1,2,3]}

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());

Create Jsonstring - Android

Hi i want to create json string like
{"id":"12345","option-ids":["100"]}
i've tried like below
JSONObject object = new JSONObject();
try {
object.put("id","12314");
JsonArray jsonArray = new JsonArray();
jsonArray......
object.put("option-ids",""+jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}
but i struck on create json array withou object name.
// First - create the json Object
JSONObject object = new JSONObject();
try {
// Add the id to the json
object.put("id", "12314");
// Create a json array
JSONArray jsonArray = new JSONArray();
// Put the values you want
jsonArray.put("1");
jsonArray.put("2");
object.put("option-ids",jsonArray.toString());
} catch (JSONException e) {
// Handle impossible error
e.printStackTrace();
}
To insert String in JSONArray,use put() method of JSONArray.
try below code.
JSONObject object = new JSONObject();
try {
object.put("id","12345");
JSONArray jsonArray = new JSONArray();
jsonArray.put("100"); //add here
object.put("option-ids",jsonArray.toString());
} catch (JSONException e) {
e.printStackTrace();
}
You should use JSONArray (from org.json) instead of JsonArray(from com.google.gson)

how to change the name of JSONObject dynamically

I have few dialog's in which the user enters their details(person details- first name, last name etc..), I need to capture the details and convert to JSON.
User can enter more than one person details like. person1, person2, person3(which should be the JSONObjects). When ever the bellow function is called for the first time I want
JSONObject personJSON1 = new JSONObject();
second time its called
JSONObject personJSON2 = new JSONObject(); and so on.
Could you please suggest how can i achieve this.
private void personAdded() {
JSONObject personJSON = new JSONObject();
JSONArray personArrayjson = new JSONArray();
JSONObject personObjectJson = new JSONObject();
try {
personObjectJson.put("otherFirstName", sOtherFirstName);
personObjectJson.put("otherLastName", sOtherLastName);
personObjectJson.put("otherAddress", sOtherAddress);
personObjectJson.put("otherTown", sOtherTown);
personObjectJson.put("otherCounty", sOtherCounty);
personObjectJson.put("otherPostcode", sOtherPostcode);
personObjectJson.put("otherTelephone", sOtherTelephone);
personObjectJson.put("otherMobilePhone", sOtherMobilePhone);
personObjectJson.put("otherEmail", sOtherEmail);
personObjectJson.put("otherPersonInvolvement", sHowWasTheOtherPersonInvolved);
} catch (JSONException e) {
e.printStackTrace();
}
}
Your help/suggestions much appreciated. Thank you
public List<JSONObject> mMyList = new ArrayList<JSONObject>();
private void personAdded() {
JSONObject personJSON = new JSONObject();
JSONArray personArrayjson = new JSONArray();
JSONObject personObjectJson = new JSONObject();
try {
personObjectJson.put("otherFirstName", sOtherFirstName);
personObjectJson.put("otherLastName", sOtherLastName);
personObjectJson.put("otherAddress", sOtherAddress);
personObjectJson.put("otherTown", sOtherTown);
personObjectJson.put("otherCounty", sOtherCounty);
personObjectJson.put("otherPostcode", sOtherPostcode);
personObjectJson.put("otherTelephone", sOtherTelephone);
personObjectJson.put("otherMobilePhone", sOtherMobilePhone);
personObjectJson.put("otherEmail", sOtherEmail);
personObjectJson.put("otherPersonInvolvement", sHowWasTheOtherPersonInvolved);
mMyList.add(personJSON)
} catch (JSONException e) {
e.printStackTrace();
}
Doing the list implementation would look something like this.

Categories

Resources