This should be the JSON output:
{
"ph_immunizations_attributes": [
{"immunization_id": 1},
{"immunization_id": 2}
]
}
How will i translate it in my Android codes to post in my rails server
And here's the code:
List<NameValuePair> immunizatioValuePairs = new ArrayList();
JSONObject jObj = new JSONObject();
JSONObject main = new JSONObject();
JSONArray jA = new JSONArray();
jObj.put("immmunization_id", 1);
jA.put(jObj);
main.put("ph_immunizations_attributes", jA);
immunizatioValuePairs.add(new BasicNameValuePair("ph_immunizations_attributes", main.toString()));
httppost.setEntity(new UrlEncodedFormEntity(immunizatioValuePairs));
JSONObject jObj = new JSONObject();
JSONObject main = new JSONObject();
JSONArray jA = new JSONArray();
jObj.put("immunization_id", 1); // 1 --> get the values here either from other class or array or whatever.
jA.put(jObj);
main.put("ph_immunizations_attributes", jA);
Try this and let me know
Related
I want to create JSONArray like this
when i add json object in array it will format like this
My Code:
JSONObject object = new JSONObject();
object.put("calories_burn", "345");
object.put("time", dp.getTimestamp(TimeUnit.MILLISECONDS));
JSONObject object1 = new JSONObject();
object1.put("calories", object);
array.put(object1);
You are falling in between two patterns, you can either use it as a map, or an array.
Map approach:
{
"steps": { "steps":123, "time": 123 },
"calories": { and so},
"bpm": { on }
}
Code (untested, think and tweak)
// Build your objects
JSONObject steps = new JSONObject();
steps.put("steps", 123);
steps.put("time" 123);
JSONObject calories = new JSONObject();
//And so on
JSONObject bpm = new JSONObject();
//Make a map
JSONObject map = new JSONObject();
//Add to the map:
map.put("steps", steps);
map.put("calories", calories);
map.put("bpm", bpm);
No, you cannot add keys inside a JSONArray. You can do that inside a JSONObject though.
Two approaches:
With JSONArray (Acts Like A List):
JSONArray jsonArray = new JSONArray();
JSONObject caloriesJSON = new JSONObject();
caloriesJSON.put("calories_burn", 345);
caloriesJSON.put("time", dp.getTimestamp(TimeUnit.MILLISECONDS));
JSONObject stepsJSON = new JSONObject();
stepsJSON.put("steps", "12121");
stepsJSON.put("time", dp.getTimestamp(TimeUnit.MILLISECONDS));
jsonArray.put(stepsJSON);
jsonArray.put(caloriesJSON);
With JSONObject(Acts Like A Map/Dictionary):
JSONObject jsonObject = new JSONObject();
JSONObject caloriesJSON = new JSONObject();
caloriesJSON.put("calories_burn", 345);
caloriesJSON.put("time", dp.getTimestamp(TimeUnit.MILLISECONDS));
JSONObject stepsJSON = new JSONObject();
stepsJSON.put("steps", "12121");
stepsJSON.put("time", dp.getTimestamp(TimeUnit.MILLISECONDS));
jsonObject.put("steps", stepsJSON);
jsonObject.put("calories", caloriesJSON);
Be careful what you put for values. "12313" is a String whereas 345 is an integer.
I need to make a JSON like this on Android -
{"LoomMachine" : ["Waterjet","Rapier"],
"LoomType" : ["Dobby","Crank"]}
Any help on how to make this ?
This is what you are looking for,
public Object JSONData() throws Exception {
JSONObject JSONObjectData = new JSONObject();
JSONArray loomMachineArr = new JSONArray();
loomMachineArr.add("Waterjet");
loomMachineArr.add("Rapier");
JSONArray LoomType= new JSONArray();
LoomType.add("Dobby");
LoomType.add("Crank");
JSONObjectData.put("LoomMachine", loomMachineArr);
JSONObjectData.put("LoomType", LoomType);
return root;
}
I need help sending JSON to server side. This is how it should look:
"myProfile": { "languages": [ "English", "German" ] }
So myProfile is a JSONObject that contains "languages" which is array of strings, right?
Can someone help me send JSON to server?
JSONObject myProfileObject= new JSONObject();
JSONObject languagesObject = new JSONObject();
String[] languagesToServer = {"English", "German"};
languagesObject.put("languages", languagesToServer);
myProfileObject.put("myProfile", languagesObject);
This is creating "myProfile": {"languages":"[Ljava.lang.String;#42b82168"} which is obviously not good.
Can someone guide me please?
JSONArray mJsonArray = new JSONArray();
mJsonArray.put("English");
mJsonArray.put("German");
JSONObject mJsonObject = new JSONObject();
mJsonObject.put("languages", mJsonArray);
JSONObject mObject = new JSONObject();
mObject.put("myProfile", mJsonObject);
System.out.println(mObject.toString());
I am trying to send this json to web server:
[{"codemenu":"1","name":"Fried Rice"},
{"codemenu":"2","name":"Hongkong Fried Rice"},
{"codemenu":"3","name":"Special fried Rice"}]
This is the code but it's not working:
HttpPost httppost = new HttpPost("http://10.0.2.2/pnir_restoran/test.php");
JSONObject json = new JSONObject();
try {
// JSON data:
json.put("codemenu", "1");
json.put("name", "friedrice");
json.put("codemenu", "2");
json.put("name", "Hongkong friedrice");
json.put("codemenu", "3");
json.put("name", "Special friedrice");
JSONArray postjson=new JSONArray();
postjson.put(json); //i cant use postjson.add(json);
// Post the data:
httppost.setHeader("json",json.toString());
httppost.getParams().setParameter("jsonpost",postjson);
// Execute HTTP Post Request
System.out.print(json);
HttpResponse response = httpclient.execute(httppost);
What should I do? Please help me.
You need to use a JSONArray and then put individual JSONObjects inside the array:
// Initialize the JSON Array and your three seperate objects.
JSONArray jsonArray = new JSONArray();
JSONObject jObj1 = new JSONObject();
JSONObject jObj2 = new JSONObject();
JSONObject jObj3 = new JSONObject();
// Put elements in one object at a time and put them in your array.
jObj1.put("codemenu", "1");
jObj1.put("name", "friedrice");
jsonArray.put(jObj1);
jObj2.put("codemenu", "2");
jObj2.put("name", "Hongkong friedrice");
jsonArray.put(jObj2);
jObj3.put("codemenu", "3");
jObj3.put("name", "Special friedrice");
jsonArray.put(jObj3);
Instead of doing all JSON handling manually, you can use Spring Android's RestTemplate module.
I am using this code for detect the source language..
jsonObj = new JSONObject(response);
JSONObject jsoObj2 = jsonObj.getJSONObject("data");
JSONArray jArray = jsoObj2.getJSONArray("detections");
JSONObject steps = jArray.getJSONObject(0);
srcLanguage = steps.getString("language");
Here is the response.
{
"data": {
"detections": [
[
{
"language": "fr",
"isReliable": false,
"confidence": 0.41935483
}
]
]
}
}
Kindly help me in order to parse this json response.
I debug that code and getting this exception.
org.json.JSONException: Value [{"isReliable":false,"confidence":0.41935483,"language":"fr"}] at 0 of type org.json.JSONArray cannot be converted to JSONObject
Change it to this:
jsonObj = new JSONObject(response);
JSONObject jsoObj2 = jsonObj.getJSONObject("data");
JSONArray jArray = jsoObj2.getJSONArray("detections");
JSONArray jArray2 = jArray.getJSONArray(0);
JSONObject steps = jArray2.getJSONObject(0);
srcLanguage = steps.getString("language");
Because there is an array in the array.
If above mentioned is the response you need to have a minor change
jsonObj = new JSONObject(response);
JSONObject jsoObj2 = jsonObj.getJSONObject("data");
JSONArray jArray = jsoObj2.getJSONArray("detections");
JSONArray steps = jArray.getJSONArray(0);
srcLanguage = jArray.getJSONObject(0).getString("language");
Try his please...