This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 5 years ago.
My JSONArray contains various JSONObjects which look like this:
{"nearby":0,"x":0,"name":["OPS","BHU"],"y":0}
{"nearby":0,"x":0,"name":["None"],"y":1}
{"nearby":0,"x":0,"name":["None"],"y":2}
{"nearby":0,"x":0,"name":["None"],"y":3}
{"nearby":0,"x":0,"name":["None"],"y":4}
{"nearby":0,"x":0,"name":["None"],"y":5}
{"nearby":0,"x":0,"name":["None"],"y":6}
{"nearby":0,"x":1,"name":["None"],"y":0}
{"nearby":0,"x":1,"name":["None"],"y":1}
{"nearby":0,"x":1,"name":["None"],"y":2}
{"nearby":0,"x":1,"name":["DDF","THG"],"y":3}
{"nearby":0,"x":1,"name":["None"],"y":4}
{"nearby":0,"x":1,"name":["None"],"y":5}
{"nearby":1,"x":1,"name":["ABC","DEF","XYZ"],"y":6}
I want to retrieve values corresponding to name key in JSONObject and from an array like this:
JSONObject jo= jsonArray.getJSONObject(i);
String nearby= jo.getString("nearby");
String xString= jo.getString("x");
int x=Integer.parseInt(xString);
String yString= jo.getString("y");
int y=Integer.parseInt(yString);
String[][] name=new String[7][7];
name[x][y]= ????????
Such that the output be like:
name[0][0]=["OPS","BHU"]
name[0][1]=["None"]
name[0][2]=["None"]
name[0][3]=["None"]
name[0][4]=["None"]
name[0][5]=["None"]
name[0][6]=["None"]
name[1][0]=["None"]
name[1][1]=["None"]
name[1][2]=["None"]
name[1][3]=["DDF","THG"]
name[1][4]=["None"]
name[1][5]=["None"]
name[1][6]=["ABC","DEF","XYZ"]
I hope this is what you are looking for,
var arr = [
{"nearby":0,"x":0,"name":["OPS","BHU"],"y":0},
{"nearby":0,"x":0,"name":["None"],"y":1},
{"nearby":0,"x":0,"name":["None"],"y":2},
{"nearby":0,"x":0,"name":["None"],"y":3},
{"nearby":0,"x":0,"name":["None"],"y":4},
{"nearby":0,"x":0,"name":["None"],"y":5},
{"nearby":0,"x":0,"name":["None"],"y":6},
{"nearby":0,"x":1,"name":["None"],"y":0},
{"nearby":0,"x":1,"name":["None"],"y":1},
{"nearby":0,"x":1,"name":["None"],"y":2},
{"nearby":0,"x":1,"name":["DDF","THG"],"y":3},
{"nearby":0,"x":1,"name":["None"],"y":4},
{"nearby":0,"x":1,"name":["None"],"y":5},
{"nearby":1,"x":1,"name":["ABC","DEF","XYZ"],"y":6},
]
var resultArr = [[],[]];
$.each(arr, function (key, item) {
resultArr[item.x][item.y]=item.name;
});
console.log(resultArr[0][0])
console.log(resultArr[0][1])
console.log(resultArr[0][5])
console.log(resultArr[1][4])
console.log(resultArr[1][6])
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Related
This question already has answers here:
Convert JSONArray to String Array
(18 answers)
Closed 2 years ago.
I want to extract authors from this JSON, I successfully get the JSONArray but I don't know how to convert it into String[], I have to pass this String[] to a method. Please try to keep the solution simple as I am just a beginner.
JSON :
{
"volumeInfo": {
"title": "Android",
"authors": [
"P.K. Dixit"
]
}
}
Code:
JSONObject volumeInfo = currentBook.getJSONObject("volumeInfo");
// Extract the value for the key called "title"
String title = volumeInfo.optString("title");
// Extract the array for the key called "authors"
JSONArray authors = volumeInfo.optJSONArray("authors");
private ArrayList<String> authorarray = new ArrayList<String>();
for (int i = 0; i < authors.length(); i++) {
String author = authors.getString(i);
//add author to your array
authorarray.add(author);
}
Use this function
String[] authors_arr(JSONArray authors){
String[] authors_res=new String[authors.length()];
try {
for(int i=0;i<authors.length();i++)
{
authors_res[i]=authors.getString(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return authors_res;
}
call it like this String[] authors_string_array = authors_arr(authors);
The main point is to loop through your json array and add it into your array
This question already has an answer here:
How to convert a map to Json string in kotlin?
(1 answer)
Closed 2 years ago.
I have a mutableMap,
val invoiceAdditionalAttribute = mutableMapOf<String, Any?>()
invoiceAdditionalAttribute.put("clinetId",12345)
invoiceAdditionalAttribute.put("clientName", "digital")
invoiceAdditionalAttribute.put("payload", "xyz")
I want to convert it into string which looks like escaped json
the output should be,
"{\"clinetId\":\"12345\", \"clientName\":\"digital\", \"payload\":\"xyz\"}"
Currently, I am using Gson library,
val json = gson.toJson(invoiceAdditionalAttribute)
and the output is
{"clinetId":12345,"clientName":"digital","payload":"xyz"}
Use replace extension function on String class:
val invoiceAdditionalAttribute = mutableMapOf<String, Any?>()
invoiceAdditionalAttribute["clinetId"] = 12345.toString()
invoiceAdditionalAttribute["clientName"] = "digital"
invoiceAdditionalAttribute["payload"] = "xyz"
val json = gson.toJson(invoiceAdditionalAttribute)
.replace("\"", "\\\"")
json now contains string:
{\"clinetId\":\"12345\",\"clientName\":\"digital\",\"payload\":\"xyz\"}
This question already has answers here:
How to parse JSON Array (Not Json Object) in Android
(11 answers)
Closed 5 years ago.
I have a Jsonobject that contain some values that were an Arraylist before converting it to Jsonobject
[{"id":4,"name":"shirt","attributeName":["size"],"attributeValue":["6-7"],"attributeStock":["3"]}]
how can get attributeName & attributeValue & attributeStock like an Arraylist
I solve it by this code (jsonFavorites is a String that contain several jsonobject same to that i post in the Question [{},{},{}])
try
{
JSONArray jsonArray=new JSONArray(jsonFavorites);
for (int i=0; i<jsonArray.length();i++)
{
JSONObject object=jsonArray.getJSONObject(i);
int id=object.getInt("id");
String name=object.getString("name");
JSONArray Namearray=object.getJSONArray("attributeName");
JSONArray Valuearray=object.getJSONArray("attributevalue");
JSONArray Stockarray=object.getJSONArray("attributeStock");
ArrayList<String> attributeName=new ArrayList<>();
ArrayList<String> attributevalue=new ArrayList<>();
ArrayList<String> attributeStock=new ArrayList<>();
for (intj=0; j<Namearray.length();j++)
{
attributeName.add(Namearray.getString(j));
attributevalue.add(Valuearray.getString(j));
attributeStock.add(Stockarray.getString(j));
}
}
}catch (Exception e){}
This question already has answers here:
How to parse JSON array from file?
(3 answers)
Closed 5 years ago.
I´ve been trying to parse this Json format but since it has no name for the array i have no idea how to parse it... Thanks for your help and time.
Im using JSON simple library as a parser.
[
{
"Name":"John",
"LastName":"Wick"
},
{
"Name":"Johny",
"LastName":"Wicked"
}
]
the code im trying to use is this one :
JSONArray jsonarray = new JSONArray("data/names.json");
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String name = jsonobject.getString("name");
String url = jsonobject.getString("url");
}
You can use Jackson built in tree model. For example like this
ObjectMapper mapper = new ObjectMapper();
JsonNode rootArray = mapper.readTree(new File("c:\\jsonfile.json"));
for(JsonNode root : rootArray) {
// your code
...
}
This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 7 years ago.
String response = "{\"mon\" : [[16,20]],\"sun\":[[10,20]]}";
Basically, how do I get one object of 2 items where in each item is an int array of 2 elements. Sample code please.
JSONArray jsonArray = jsonObject.getJSONArray("mon");
JSONArray weekdayArray = jsonArray.getJSONArray(0);
int size = weekdayArray.length();
String weekday[] = new String[size];
int[] wday = new int[size];
for(int i=0; i < size ; i++) {
weekday[i] = weekdayArray.getString(i);
wday[i] = Integer.parseInt(weekday[i]);
}
Where you get this json? because this json format is incorrect,if you necessary validate this json , access the http://json.parser.online.fr/ e paste your json e verify if this json is correct.