send string[] as JSON - android

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

Related

Create JSON Array with object name android

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.

Making JSON including JSONObject and JSONArray

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

Passing Arrays in array JSON from Android to rails server

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

how to build Below kind of JSON Format to post it as a request in android?

Ineed to post data in JSON format as shown below And the size of the array is not
constant. it depends on the user.
{
"info": [
{
"Name": "foo1",
"Number": 123
},
{
"Name": "foo2",
"Number": 124
},
{
"Name": "foo2",
"Number": 125
}
]
}
I tried to create in the following way
JSONObject parent = new JSONObject();
JSONArray jArray = new JSONArray();
JSONObject childObj = new JSONObject();
childObj.put("Name", etName.getText()).toString();
childObj.put("Number", etNumber.getText()).toString();
jArray.put(childObj);
parent.put("info",jArray);
but i'm unable to get it and I also tried in this way like example 6.1 but there is no method like add for JSONArray. So how can post my data. Please help me.
Thanks.
I'm not sure exactly what your question is but I'd typically do something like this to create the JSON string that you are looking for (the example below is using the org.json reference implementation from http://www.json.org/java/index.html):
JSONObject parent = new JSONObject();
JSONArray infoArray = new JSONArray();
for(int i = 0; i < SOMEARRAY.length; i++)
{
JSONObject childObj = new JSONObject();
childObj.put("Name", etName.getText());
childObj.put("Number", etNumber.getText());
infoArray.put(childObj);
}
parent.put("info", infoArray);
String encodedJsonString = parent.toString();
I don't understand why you're calling toString() after putting stuff into childObj.
Anyway, this code should work:
JSONObject parent = new JSONObject();
JSONArray jArray = new JSONArray();
JSONObject childObj = new JSONObject();
try
{
//create first child
childObj
.put("Name", "foo1")
.put("Number", 123);
//insert it into the array
jArray.put(childObj);
//... repeat for all remaining elements
//attach the array to the parent
parent.put("info",jArray);
}
catch (JSONException e)
{
e.printStackTrace();
}
Then, to get your JSON as a string for POSTing just call parent.toString();

create json in android

I wish to create json looking like:
{"man":
{
"name":"emil",
"username":"emil111",
"age":"111"
}
}
within android. This is what I have so far:
JSONObject json = new JSONObject();
json.put("name", "emil");
json.put("username", "emil111");
json.put("age", "111");
Can anyone help me?
You can put another JSON object inside the main JSON object:
JSONObject json = new JSONObject();
JSONObject manJson = new JSONObject();
manJson.put("name", "emil");
manJson.put("username", "emil111");
manJson.put("age", "111");
json.put("man",manJson);

Categories

Resources