Im making an app thats dynamic to a json template (the app reads the template from the internet) and i need to read a JSONArray's key string.
my json:
{
"format": {
"Drive": [
"Mecanum",
"Omni",
"Regular"
],
"Drive-Configuration": [
"H",
"X"
],
"Glyph-Elevator": [
"Parallel Elevator",
"Stick Elevator",
"Strip Elevator"
],
"Glyph-Picker": [
"Strip Picker",
"Dual-Servo Picker"
],
"Relic-Elevator": [
"Horizontal Parallel"
],
"Relic-Holder": [
"Pliers",
"Dual-Servo With Rubber Pads"
],
"CypherBox-Fill": [
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12"
],
"Autonomous": [
"Poor",
"Minimal",
"Okay",
"Good",
"Almost Perfect",
"Perfect"
]
}
}
what i want is to read the "Drive" and the "Drive-Configuration" names by code.
what i have:
JSONObject reader=new JSONObject(template);
JSONArray config=reader.getJSONArray("format");
for(int type=0;type<config.length();type++){
JSONArray con=config.getJSONArray(type);
//Here I Want To Read The Array's Name
}
Instead of JSON Parser use GSON.!
add this to your gradle
compile 'com.google.code.gson:gson:2.8.1'
Create a ClassMain.!
class MainClass{
#SerializedName("format")
Value format;
}
Class value{
#SerializedName("Drive")
ArrayList<String> drive;
#SerializedName("Drive-Configuration")
ArrayList<String> driveConfiguration;
generate getter and setter.!
}
and then Convert your JSON to GSON.!
MainClass mainClass= new Gson().fromJson(json.toString(), MainClass.class);
mainClass.getFormat().getDirve();
Used JSONObject.names();
to read the names
ArrayList<Template> tm = new ArrayList<>();
JSONObject reader = new JSONObject(format);
JSONObject config = reader.getJSONObject("format");
Iterator<String> types = config.keys();
while (types.hasNext()) {
String name = types.next();
tm.add(new Template(name, config.getJSONArray(name)));
}
Related
{
"result": "success",
"response_code": 200,
"message": "data list",
"data": [
{
"Assigned": "14",
"Closed": "150",
"Escalated TTO": "102",
"Escalated TTR": "2",
"New": "44",
"Pending": "4",
"Resolved": "14"
}
]
}
Above is the response that I want to parse, but I don't want to create a static POJO for it. The model should be dynamic so that even if there is another key-value pair in the above response, like as shown below, the response is able to be parsed I guess it is possible using HashMap.
{
"result": "success",
"response_code": 200,
"message": "data list",
"data": [
{
"Assigned": "14",
"Closed": "150",
"Escalated TTO": "102",
"Escalated TTR": "2",
"New": "44",
"Pending": "4",
"Resolved": "14",
"xx": "xx",
"xx": "xx",
"xx": "xx",
}
]
}
As discussed in the comments, the ideal scenario would be a Map<String, String>. The models could look like (assuming you're using Gson. Should be similar for others):
data class ApiResponse(
#SerializedName("result")
val result: String,
#SerializedName("responseCode")
val responseCode: Int,
#SerializedName("message")
val message: String,
#SerializedName("data")
val data: List<Map<String, String>>
)
So, I have the next json:
{
"massive2":[
"a",
"c",
"b"
],
"key5":"val5",
"key2":"val2",
"massive1":[
"3",
"4",
"2",
"1"
],
"key3":"val3",
"key4":"val4",
"key1":"val1"
}
How can I sort all pairs by key and all elements in all massives (to the end of json tree) in alphabetical, or if it is massive in some other order to get json like this:
{
"key1":"val1",
"key2":"val2",
"key3":"val3",
"key4":"val4",
"key5":"val5",
"massive1":[
"1",
"2",
"3",
"4"
],
"massive2":[
"a",
"b",
"c"
]
}
I don't know beforehand how deep my json will be.
Maybe there are some java libraries for this?
let items = {
"massive2":[
"a",
"c",
"b"
],
"key5":"val5",
"key2":"val2",
"massive1":[
"3",
"4",
"2",
"1"
],
"key3":"val3",
"key4":"val4",
"key1":"val1"
};
let data = {};
Object.keys(items).sort().forEach(function(k){
data[k] = items[k];
});
console.log(data);
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());
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/