how to change the name of JSONObject dynamically - android

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.

Related

JSONArray and ArrayList getting replaced for every value insertion

JSONArray getting replaced for every value insertion
In this below output I am getting replaced by old jArrayFacebookData.
or I need those json data should be add to string
dbchooseuserid,dbchoosebookid,dbchoosechapterid,dbchoosequestionid, Type,dbchooseoptionidthose valus are from db with those values I need to make json string
I need output like this
string h =[{"user_id":"1","book_id":"64","chapter_id":"1","question_type":"hint","question_id":"1","notes":",,Gg,,,,"},{"user_id":"1","book_id":"64","chapter_id":"1","question_type":"choose","question_id":"1","option_id":"1"}]
This is my code
JSONArray jArrayFacebookData = new JSONArray();
JSONObject jObjectData = new JSONObject();
ArrayList<String> a= new ArrayList<String>();
if(TypeHint.equals("hint"))
{
try {
jObjectData.put("user_id", dbhintuserid);
jObjectData.put("book_id", dbhintbookid);
jObjectData.put("chapter_id", dbhintchapterid);
jObjectData.put("question_type", TypeHint);
jObjectData.put("question_id", dbhintquestionid);
jObjectData.put("notes", dbhintnotes);
jArrayFacebookData.put(jObjectData);
Log.i("tag555", ""+jArrayFacebookData.toString());
a.add(jObjectData.toString());
Log.i("tag5", ""+a);
} catch (JSONException e) {
e.printStackTrace();
}}
if(Type.equals("choose"))
{
try {
jObjectData.put("user_id", dbchooseuserid);
jObjectData.put("book_id", dbchoosebookid);
jObjectData.put("chapter_id", dbchoosechapterid);
jObjectData.put("question_type", Type);
jObjectData.put("question_id", dbchoosequestionid);
jObjectData.put("option_id", dbchooseoptionid);
jArrayFacebookData.put(jObjectData);
Log.i("tag555", ""+jArrayFacebookData.toString());
a.add(jObjectData.toString());
Log.i("tag5", ""+a);
} catch (JSONException e) {
e.printStackTrace();
}}
Output I am getting is
[{"user_id":"1","book_id":"64","chapter_id":"1","question_type":"choose","question_id":"1","option_id":"1"}]
But I need output like this
[{"user_id":"1","book_id":"64","chapter_id":"1","question_type":"hint","question_id":"1","notes":",,Gg,,,,"}{"user_id":"1","book_id":"64","chapter_id":"1","question_type":"choose","question_id":"1","option_id":"1"}]
Instantiate JSONObject after every if condition and then try.
if(TypeHint.equals("hint")){
JSONObject jObjectData = new JSONObject();
......
if(Type.equals("choose")){
JSONObject jObjectData = new JSONObject();
....

How to convert JSONObject to JSONArray in 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())));

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 create a JSON

I was create a JSON Object like below but i don't know how can fill parameters field with JSONStringer? And parameters field is a JSONArray or Array String?
{"name":"Katy", "parameters":["JAK","1999"], "Age":25}
Thanks for your help .
Try like below
String mParameters[] = { "JAK", "1999" };
JSONObject mJson = new JSONObject();
try {
mJson.put("name", "Katy");
JSONArray mJSONArray = new JSONArray(Arrays.asList(mParameters));
mJson.putOpt("parameters", mJSONArray);
mJson.put("Age", 25);
System.out.println("JSon::"+ mJson.toString());
} catch (JSONException e) {
e.printStackTrace();
}
The documentation for JSON will show you that an easy way to distinguish wheter your JSON string is an array is whether it starts with a [, so "parameters" in your example is a JSONArray.
Without knowing how you will be getting the data you want to put in your object, here is an example of how you would populate it (assuming you have an array of JAKS to insert).
JSONObject yourObject = new JSONObject();
String[] JAKS = {"1999", "2000", "2001"};
JSONArray paramaters = new JSONArray();
try {
yourObject.put("name", "Katy");
for (String JAK : JAKS) {
JSONObject yourParamater = new JSONObject();
yourParamater.put("JAK", JAK);
paramaters.put(yourParamater);
}
yourObject.put("parameters", paramaters);
yourObject.put("Age", 25);
} catch (JSONException e) {
e.printStackTrace();
}
Try this:
import org.json.JSONObject;
//other imports
//...
try {
//Create the JSON Object
JSONObject myObject = new JSONObject();
String parameters[] = new String[]{"JAK","1999"};
//use the method put to "fill" the values
myObject.put("name", "Katy");
myObject.put("parameter",(Object)parameters);
myObject.put("age", 25);
} catch (JSONException e) {
e.printStackTrace();
}
Try this way,hope this will help you to solve your problem.
try{
JSONObject jsonObject = new JSONObject();
jsonObject.put("name","Katy");
jsonObject.put("parameters",new String[]{"JAK","1999"});
jsonObject.put("Age","25");
}catch (Throwable e){
e.printStackTrace();
}

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)

Categories

Resources