Making JSON including JSONObject and JSONArray - android

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

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.

Parsing JsonArray With JsonObject

I have some JSON with the following structure:
{
"items":[
{
"product":{
"product_id":"some",
"price_id":"some",
"price":"some",
"title_fa":"some",
"title_en":"Huawei Ascend Y300",
"img":"some",
"has_discount_from_price":"0",
"discount_from_price":null,
"type_discount_from_price":null,
"has_discount_from_product":"0",
"discount_from_product":null,
"type_discount_from_product":null,
"has_discount_from_category":"0",
"discount_from_category":null,
"type_discount_from_category":null,
"has_discount_from_brand":"0",
"discount_from_brand":null,
"type_discount_from_brand":null,
"weight":null,
"features":[
{
"feature_value":"#000000",
"feature_id":"some",
"feature_title":"some"
},
{
"feature_value":"some",
"feature_id":"1652",
"feature_title":"some"
}
]
},
"number":1,
"feature_id":"56491,56493",
"price_inf":{
"has_discount":0,
"discount_type":0,
"final_price":"400000",
"value_discount":0
},
"cart_id":13
}
]
}
I'm trying to access the elements "product_id" and "price_id" with the following Java code:
try{
JSONArray feedArray=response.getJSONArray("items");
for (int i=0;i<feedArray.length();i++){
JSONObject feedObj=feedArray.getJSONObject(i);
JSONObject pro=feedObj.getJSONObject("product");
Product product = new Product();
product.setPrice(pro.getDouble("price_id"));
product.setTitle_fa(pro.getString("price_id"));}}
but i see product not found error.what is wrong in my parser?
First of all your JSON is valid. So no worries there.
Now regarding your problem, because you haven't posted the logs so I can't tell what the exact problem is. But using this code snippet you can get the desired values.
try {
JSONArray itemsJsonArray = jsonObject.getJSONArray("items");
for (int i = 0; i < itemsJsonArray.length(); i++){
JSONObject itemJsonObject = itemsJsonArray.getJSONObject(i);
JSONObject productObject = itemJsonObject.getJSONObject("product");
String productId = productObject.getString("product_id");
String priceId = productObject.getString("price_id");
}
} catch (JSONException e) {
e.printStackTrace();
}
Validate and create Pojo for your json here
use
Data data = gson.fromJson(this.json, Data.class);
follow https://stackoverflow.com/a/5314988/5202007
By the way your JSON is invalid .
you are getting a json object from your response not json array you need to make following changes
JSONObject temp =new JSONObject(response);
JSONArray feedArray=temp.getJSONArray("items");
Try converting response string to JSONObject first
try{
JSONObject temp =new JSONObject(responseString); // response is a string
JSONArray feedArray=.getJSONArray("items");
....
}
You may try to use GSON library for parsing a JSON string. Here's an example how to use GSON,
Gson gson = new Gson(); // Or use new GsonBuilder().create();
MyType target = new MyType();
String json = gson.toJson(target); // serializes target to Json
MyType target2 = gson.fromJson(json, MyType.class); // deserializes json into target2

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

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

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