Android build a jsonarray of jsonobjects - android

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

Related

Android - How to parse intent to json object?

I want to add my intent to json object, but how to do that with this code below
Pojo.java
//getter and setter on above code
JSONObject obj = new JSONObject();
try {
obj.put("id", id);
obj.put("nilai", ratingStar);
} catch (JSONException e) {
e.printStackTrace();
}
return obj;
}
Main.java
for (int i=0;i<size;i++){
jArray.put(adapter3.getItem(i).getJsonObject());
}
JSONObject j=new JSONObject();
try {
j.put("fishing",jArray);
Log.d("json output : ",j.toString());
} catch (JSONException e) {
e.printStackTrace();
}
And the respone from Log.d("json output : ",j.toString()); is :
{"fishing":[{"id":"1","nilai":1},{"id":"2","nilai":1},{"id":"3","nilai":1},{"id":"4","nilai":1},{"id":"5","nilai":1}]}
But i want to add my intent into json object like this
{"fishing":[{|"id_dosen":"2","id_matkul":"3"|,"id":"1","nilai":1},{|"id_dosen":"2","id_matkul":"3"|,"id":"2","nilai":1},{|"id_dosen":"2","id_matkul":"3"|,"id":"3","nilai":1},{|"id_dosen":"2","id_matkul":"3"|,"id":"4","nilai":1},{|"id_dosen":"2","id_matkul":"3"|,"id":"5","nilai":1}]}
can someone help me how to add the json that I mark with |??
Note : i use | symbol just to let you guys easy to read what's the different from 1st json with 2nd json
You can simply add those fields in your JSON like this.
String jsonFishing = "{\"fishing\":[{\"id\":\"1\",\"nilai\":1},{\"id\":\"2\",\"nilai\":1},{\"id\":\"3\",\"nilai\":1},{\"id\":\"4\",\"nilai\":1},{\"id\":\"5\",\"nilai\":1}]}";
try {
JSONObject rootObj = new JSONObject(jsonFishing);
JSONArray fishingArr = rootObj.getJSONArray("fishing");
for (int i = 0; i < fishingArr.length(); i++){
JSONObject child = fishingArr.getJSONObject(i);
child.put("id_dosen", 2);
child.put("id_matkul", 3);
}
...
yourIntent.putExtra("json", rootObj.toString());
} catch (JSONException e) {
e.printStackTrace();
}
Newly obtained JSON looks like this.
{"fishing":[{"id":"1","nilai":1,"id_dosen":2,"id_matkul":3},{"id":"2","nilai":1,"id_dosen":2,"id_matkul":3},{"id":"3","nilai":1,"id_dosen":2,"id_matkul":3},{"id":"4","nilai":1,"id_dosen":2,"id_matkul":3},{"id":"5","nilai":1,"id_dosen":2,"id_matkul":3}]}
Edit : In your code, it would be like this
for (int i=0;i<size;i++){
JSONObject jsonObject = adapter3.getItem(i).getJsonObject();
jsonObject.put("id_dosen", 2);
jsonObject.put("id_matkul", 3);
jArray.put(jsonObject);
}
Hope this helps :)

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

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