create json in android - 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);

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

send string[] as JSON

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

Parsing multiple Json Objects in android

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.

How to parse json url using android?

I want parse json url,my json url contains following structures
{"content":{"item":[{"category":"xxx"},{"category":"yy"} ]}}
how to read this structure,anybody knows give example json parser for that.
Thanks
This code will help you to parse yours json.
String jsonStr = "{\"content\":{\"item\":[{\"category\":\"xxx\"},{\"category\":\"yy\"} ]}}";
try {
ArrayList<String> categories = new ArrayList<String>();
JSONObject obj = new JSONObject(jsonStr);
JSONObject content = obj.getJSONObject("content");
JSONArray array = content.getJSONArray("item");
for(int i = 0, count = array.length();i<count;i++)
{
JSONObject categoty = array.getJSONObject(i);
categories.add(categoty.getString("category"));
}
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject can do the parsing.
You need to use the org.json's package.
Example:
For an object:
JSONObject json = new JSONObject(json_string);
For an array:
JSONArray json = new JSONArray(json_string);

Categories

Resources