I have the following code:
JSONObject student2 = new JSONObject();
try {
student2.put("name", "NAME OF STUDENT2");
System.out.println(student2.get("name"));
System.out.println(student2.getString("name"));
} catch (JSONException e) {
e.printStackTrace();
}
I was just trying to create an JSONObject and extract the "name" value from it.
But the two "System.out.println" commands returns null. I expect the string "NAME OF STUDENT2" as a result.
What I was doing wrong?
try {
JSONObject jsonObject = new JSONObject("{\"name\":\"NAME OF STUDENT2\"}");
System.out.println(jsonObject.getString("name"));
}catch (JSONException err){
Log.d("Error", err.toString());
}
Related
I am trying to send json to HTTP server with this JSON format:
{"deviceID":3852883413434, "depth":2,"location":{"xCord":46.232, "yCord":42.4421},"size":3}
and here is my code in android, but seems not working well.Maybe i need array for location or format my code.Any adivce could help thanks.
JSONObject postData = new JSONObject();
try {
postData.put("deviceID", unique_id);
postData.put("depth",((HoleSize)this.getApplication()).getDepth());
postData.put("xCord",((HoleSize)this.getApplication()).getLattitude());
postData.put("yCord",((HoleSize)this.getApplication()).getLongitude());
postData.put("size", ((HoleSize) this.getApplication()).getSize());
new SendData().execute("servername",postData.toString());
} catch (JSONException e) {
e.printStackTrace();
}
Using the json response,
{"deviceID":3852883413434, "depth":2,"location":{"xCord":46.232,
"yCord":42.4421},"size":3}
you must get the JSON Object to get the value of depth and size (contained in the same object) and then get the JSON Object of location to get the values of xCord, yCord:
try {
JSONObject postData = new JSONObject(response);
String depth = postData.get("depth").toString();
String size = postData.getInt("size").toString();
JSONObject locationObject = new JSONObject(postData.get("location").toString());
String xCord = locationObject.get("xCord").toString();
String yCord = locationObject.get("yCord").toString();
} catch (JSONException e) {
e.printStackTrace();
}
applying this to your code:
try {
JSONObject postData = new JSONObject(response);
postData.put("deviceID", unique_id);
postData.put("depth",postData.get("depth").toString());
//postData.put("xCord", locationObject.get("xCord").toString());
postData.put("location", postData.get("location").toString());
postData.put("yCord",locationObject.get("yCord").toString());
postData.put("size", postData.getInt("size").toString());
new SendData().execute("servername",postData.toString());
} catch (JSONException e) {
e.printStackTrace();
}
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 :)
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);
}
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();
}
Json string contained in String str:
{conf:{"quantity_uom":"l",price_uom:"euro",distance_uom:"km",consumption_uom:"km/l"}}
Code:
try{
if (str!=""){
this.json = new JSONObject(str);
this.quantityUom=this.json.getString("quantity_uom");
this.distanceUom=this.json.getString("distance_uom");
this.priceUom=this.json.getString("price_uom");
this.consumptionUom=this.json.getString("consumption_uom");
}
}catch (Exception e) {
throw e;
}
I have returned No value for quantity_uom. How I am doing wrong? String contain the json text.
Thank you so much.
try{
if (!str.equals("")){
Json obj = new JSONObject(str);
this.json = obj.getJSONObject("conf");
this.quantityUom=this.json.getString("quantity_uom");
this.distanceUom=this.json.getString("distance_uom");
this.priceUom=this.json.getString("price_uom");
this.consumptionUom=this.json.getString("consumption_uom");
}
}catch (Exception e) {
throw e;
}
You first have to get json object named 'conf' from string and from that json object get other values.
The JSON string is not correct.
Try this:
{"conf":{"quantity_uom":"l","price_uom":"euro","distance_uom":"km","consumption_uom":"km/l"}}