I want to create a json string like below.
"data": [{
"id": "1",
"data": "one",
"color": "008ee4"
},{
"id": "2",
"data": "two",
"color": "008ee4"
}]
So far I have come up with this.
<%
JSONObject outerObject = new JSONObject();
JSONArray outerArray = new JSONArray();
JSONObject innerObject = new JSONObject();
JSONArray innerArray = new JSONArray();
innerObject.put("label", "1");
innerObject.put("value", "one");
innerObject.put("color", "008ee4");
outerObject.put("data", outerArray);
System.out.println(outerObject.toString());
%>
And it gives the output like
{"data":[]}
I want to remove those curly brackets and input data inside square brackets. Please help me.
UPDATE
{
type: "line",
renderAt: "chartContainer1",
width: "500",
height: "300",
dataFormat: "json",
"dataSource": {
"chart": {
"caption": "Total Revenues from 2008-2013",
"numberprefix": "$",
"bgcolor": "FFFFFF",
"showalternatehgridcolor": "0",
"plotbordercolor": "008ee4",
"plotborderthickness": "3",
"showvalues": "0",
"divlinecolor": "CCCCCC",
"showcanvasborder": "0",
"tooltipbgcolor": "00396d",
"tooltipcolor": "FFFFFF",
"tooltipbordercolor": "00396d",
"numdivlines": "2",
"yaxisvaluespadding": "20",
"anchorbgcolor": "008ee4",
"anchorborderthickness": "0",
"showshadow": "0",
"anchorradius": "4",
"chartrightmargin": "25",
"canvasborderalpha": "0",
"showborder": "0"
},
"data": [
{
"label": "2009",
"value": "4400000",
"color": "008ee4"
},
{
"label": "2010",
"value": "4800000",
"color": "008ee4"
},
{
"label": "2011",
"value": "5500000",
"color": "008ee4"
},
{
"label": "2012",
"value": "6700000",
"color": "008ee4",
"anchorradius": "7",
"tooltext": "Historical high"
},
{
"label": "2013",
"value": "4200000",
"color": "008ee4"
}
]
}
}
Above is the main json file. I just want to replace the data part with my data.
Do it like this:
JSONObject outerObject = new JSONObject();
JSONArray outerArray = new JSONArray();
JSONObject innerObject = new JSONObject();
JSONArray innerArray = new JSONArray();
innerObject = new JSONObject();
innerObject.put("label", "1");
innerObject.put("value", "one");
innerObject.put("color", "008ee4");
innerArray.add(innerObject);
outerObject.put("data", innerArray);
System.out.println(outerObject.toString());
Related
I have the following json, how can i convert it to arrayList?
"data": [{
"label": "Venezuela",
"value": "290"
}, {
"label": "Saudi",
"value": "260"
}, {
"label": "Canada",
"value": "180"
}, {
"label": "Iran",
"value": "140"
}, {
"label": "Russia",
"value": "115"
}, {
"label": "UAE",
"value": "100"
}, {
"label": "US",
"value": "30"
}, {
"label": "China",
"value": "30"
}]
}
You need to convert it manually iterate each json and assign it to an object then you can add it to a collection.
or even you can use Gson Library take a look at the following, gson can map your attribute into Java Object.
JsonElement yourJson = mapping.get("data");
Type listType = new TypeToken<List<YourModel>>() {}.getType(); //YourModel should contains the same attributes which the json have (Exactly)
List<String> yourList = new Gson().fromJson(yourJson, listType);
I have the GSON library in my project, I need the ext_id. I am able to access "images" using the JSON.get("images"), however I am unable to get into the images array. Can someone please help me with this.
{
"images": [{
"attributes": {},
"ext_id": "467152316",
"flag_count": 0,
"height": 2064,
"id": "da2d2307b6b72afcfbd6eab8287fad49fce442d6",
"platform": "Web",
"state_name": "public",
"type_name": "ypid",
"user_id": 7297580,
"user_type": "obile",
"width": 1161,
"verified": 1,
"blob_media_type": "image",
"created_at": "2016-08-19 19:29:13 +0000",
"deleted": 0,
"end_date": "2216-07-02 19:29:13 +0000",
"primary": 0,
"recurring": 0,
"start_date": "2016-08-19 19:29:13 +0000",
"state": 0,
"tags": [],
"type_id": 1,
"updated_at": "2016-08-19 19:29:13 +0000",
"user": "Fanta C.",
"business": {
"name": "Porto's Bakery",
"ypid": "467152316",
"listing_id": "467152316",
"primary_collection": "food",
"heading_code": "8004219",
"mip_url": "/glendale-ca/mip/portos-bakery-467152316",
"phone_number": "(818) 956-5996",
"address": "315 N Brand Blvd",
"heading_text": "Dessert Restaurants",
"city": "Glendale",
"state": "CA",
"zip": "91203",
"is_paid": false,
"tier": 999,
"source": "MDM",
"rating_attributes": [{
"name": "Atmosphere",
"id": 1031
}, {
"name": "Food",
"id": 1023
}, {
"name": "Service",
"id": 1038
}]
}
}],
"count": 1
}
You can try this one.
public void parse(String jsonString) {
JsonElement jelement = new JsonParser().parse(jsonString);
JsonObject jobject = jelement.getAsJsonObject();
JsonArray jarray = jobject.getAsJsonArray("images");
for(int i=0;i<jarray.size();i++){
JsonObject jsonObject = jarray.get(i).getAsJsonObject();
String result = jsonObject.get("ext_id").toString();
System.out.println(result);
result = jsonObject.get("height").toString();
System.out.println(result);
}
}
You can also look here
JsonArray jsonArray = (JsonArray)jObject3.get("images");
JsonObject jObject4 =(JsonObject) jsonArray.get(0);
System.out.println(jObject4.get("ext_id"));
System.out.println(jObject4.get("full_image_path"));
I am having this in JSONArray ary2;
JSON string is :
[
{
"item_id": "1",
"Head_item_id": "1",
"major_item_id": "1",
"Quantity": "10",
"selling_prize": "20",
"MRP": "90",
"title": "JK Lakshmi PPC Cement",
"SKU": "B2WBUICEM2"
},
{
"item_id": "2",
"Head_item_id": "1",
"major_item_id": "1",
"Quantity": "10",
"selling_prize": "30",
"MRP": "80",
"title": "JK Lakshmi PPC Cement",
"SKU": "B2WBUICEM2"
},
{
"item_id": "3",
"Head_item_id": "1",
"major_item_id": "1",
"Quantity": "10",
"selling_prize": "10",
"MRP": "70",
"title": "Shree Ultra OPC 43 Grade Cement",
"SKU": "B2WBUICEM5"
}
]
I want to get String array for each attribute , like :
String[] item_id;
String[] Head_item_id;
etc.
unable to get it. Please help.
Try This:
JSONArray arr = new JSONArray(yourJSONresponse);
String[] item_id=new String[array.length()];
String[] Head_item_id=new String[arr .length()];
for(int i = 0; i < arr.length(); i++){
item_id[i]=arr.getJSONObject(i).getString("item_id");
Head_item_id[i]=arr.getJSONObject(i).getString("Head_item_id");
}
You need to follow these steps:
-> Create POJO for your JSON data and provite getters and setters for each attribute.
-> Parse your JSON data using GSON
-> get the desired attribute(using getters) and store it in the respective string array
My data is like this:
{
"region":
["{'price':'119','volume':'20000','pe':'0','eps':'4.22','week53low':'92','week53high':'134.4','daylow':'117.2','dayhigh':'119.2','movingav50day':'115','marketcap':'0','time':'2015-11-25 05:13:34.996'}",
"{'price':'112','volume':'20000','pe':'0','eps':'9.22','week53low':'92','week53high':'134.4','daylow':'117.2','dayhigh':'119.2','movingav50day':'115','marketcap':'0','time':'2015-11-25 05:13:34.996'}",
"{'price':'118','volume':'20000','pe':'0','eps':'1.22','week53low':'92','week53high':'134.4','daylow':'117.2','dayhigh':'119.2','movingav50day':'115','marketcap':'0','time':'2015-11-25 05:13:34.996'}"
]
}
I am doing like below;
JSONObject jsonObj = new JSONObject(jsonString);
JSONArray regionArray = jsonObj.getJSONArray("region");
How do I get each price..
for (int i = 0; i < regionArray.length(); i++) {
JSONObject item = regionArray.getJSONObject(i);
System.out.println(item.getString("price"));
}
Caused by: com.gemstone.org.json.JSONException: JSONArray[0] is not a JSONObject.
You have an array of Strings, not of JSON objects, so you can't do JSON object methods on it.
In order for your code to work, your JSON would have to look like this:
{
"region": [
{
"price": "119",
"volume": "20000",
"pe": "0",
"eps": "4.22",
"week53low": "92",
"week53high": "134.4",
"daylow": "117.2",
"dayhigh": "119.2",
"movingav50day": "115",
"marketcap": "0",
"time": "2015-11-25 05:13:34.996"
},
{
"price": "112",
"volume": "20000",
"pe": "0",
"eps": "9.22",
"week53low": "92",
"week53high": "134.4",
"daylow": "117.2",
"dayhigh": "119.2",
"movingav50day": "115",
"marketcap": "0",
"time": "2015-11-25 05:13:34.996"
},
{
"price": "118",
"volume": "20000",
"pe": "0",
"eps": "1.22",
"week53low": "92",
"week53high": "134.4",
"daylow": "117.2",
"dayhigh": "119.2",
"movingav50day": "115",
"marketcap": "0",
"time": "2015-11-25 05:13:34.996"
}
]
}
I just replaced that automatically in a text editor. You can actually let the numbers out of their "" enclosure if you want.
Have a look at it here: http://www.jsoneditoronline.org/
And here's some info on Json: http://www.json.org/
I can't understand, why I have this error:
04-24 22:11:51.263: W/System.err(27504): org.json.JSONException: Value
<!--HERE JSON VALUE--> at data of type org.json.JSONObject cannot be
converted to JSONArray
This is my code:
JSONObject getProgile = null;
try {
//get json
getProgile = new JSONObject(CustomHttpClient.executeHttpGet(profileGetURL).toString());
//convert array
JSONArray array = getProgile.getJSONArray("data");
for (int i = 0; i < array.length(); i++) {
JSONObject c = array.getJSONObject(i);
//get TAG_CUSTOMER
JSONObject customer = c.getJSONObject("Customer");
pName = customer.getString("name");
pLname = customer.getString("name");
}
UPD:
My json
{
"status": "success",
"data": {
"Customer": {
"id": "33",
"company_id": "1",
"name": "SDfsdf",
"birthdate": "14.02.1989",
"email": "dsfsdf#sf.ff",
"photo": "/files/clients_photos/33/(null)",
"bonuses": "50",
"created": "2015-02-14 12:22:46",
"modified": "2015-02-14 12:22:46",
"ref_id": null,
"ref_code": "6363696029",
"banned": null,
"ban_reason": null,
"ban_ending": null
},
"CustomerVisit": [],
"CustomerBonus": [
{
"id": "29",
"customer_id": "33",
"user_id": "4",
"product_id": null,
"operation": "plus",
"amount": "50",
"subject": "Загрузка фото при регистрации.",
"remain": null,
"modified": "2015-02-14 12:22:46",
"date": "14.02.2015",
"created": "14.02.2015 12:22"
}
],
"CustomerCar": [
{
"id": "41",
"customer_id": "33",
"car_brand_id": "9",
"car_model_id": "11530",
"year": "2020",
"vin": "sdfsdfsdf",
"photo": "",
"number": "dsfsdf",
"created": "2015-02-14 12:22:46",
"modified": "2015-02-14 12:22:46",
"car_brand_name": "BMW",
"car_model_name": "323"
}
],
"CustomerPhone": [
{
"id": "41",
"customer_id": "33",
"phone": "+380990010222",
"created": "2015-02-14 12:22:46",
"modified": "2015-02-14 12:22:46"
}
],
"Insurance": [],
"Event": [],
"Review": [],
"Reservation": []
}
}
JSON shows data is JSONObject rather than JSONArray, so call getProfile.getJSONObject ("data")
The problem is that you are trying to convert and JSON Object with JSON array therefore an error will occur.
If you just want to get the name then you can do this:
getProgile = new JSONObject(CustomHttpClient.executeHttpGet(profileGetURL).toString());
JSONObject obj1 = getProgile.getJSONObject("data");
JSONObject obj2 = array.getJSONObject("Customer");
String name = obj2.getString("name");