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 answers here:
How can I turn a JSONArray into a JSONObject?
(4 answers)
Closed 4 years ago.
I am writing a rest java service. I want to convert my JSONArray to JSONObject and return it. But I am getting "{}" as output when I hit my rest service from browser. Although it is printing fine inside rest service when i tried to print using System.out.println();
PreparedStatement dimDelPS = null;
ResultSet dimDelRS = null;
dimDelPS = connection.prepareStatement("select * from abc");
dimDelRS = dimDelPS.executeQuery();
String dimLow=null;
while (dimDelRS.next()) {
int total_rows = dimDelRS.getMetaData().getColumnCount();
for (int i = 0; i < total_rows; i++) {
org.json.JSONObject obj = new org.json.JSONObject();
obj.put(dimDelRS.getMetaData().getColumnLabel(i + 1)
.toLowerCase(), dimDelRS.getObject(i + 1));
jsonArray.put(obj);
}
}
System.out.println("json1 :"+jsonArray);
//Sample output at this stage: ["{\"employee\":\"ANTHONY.DUNNE\"}","{\"type\":\"Manager\"}"]
dimDelRS.close();
dimDelPS.close();
JSONObject jsobobject= new JSONObject();
jsobobject.put("aoColumnDefs",jsonArray);
System.out.println(jsobobject);
return jsobobject;
You can't just return JSONObject.
You need to male sure you Marshall it into json.
'return Response.ok(jsonObject.toString(), MediaType.APPLICATION_JSON).build();'
Browser understand String, and not java object.
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 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>