Accessing members of items in a JSONArray with Java
Have you tried using JSONArray.getJSONObject(int), and JSONArray.length() to create your for-loop:
for (int i = 0; i < recs.length(); ++i) {
JSONObject rec = recs.getJSONObject(i);
int id = rec.getInt("id");
String loc = rec.getString("loc");
// ...
}
Accessing elements in JSON array
First of all, please have a look at this answer. Using org.json you can do smth. like that:
JSONObject obj = new JSONObject("Content of your string here");
JSONArray rows = obj.getJSONArray("rows");
for (int i = 0; i < rows.length(); i++) {
JSONArray elements = rows.getJSONObject(i).getJSONArray("elements");
for(int j = 0; j < elements.length(); j++) {
JSONObject element = elements.getJSONObject(j);
JSONObject duration = element.getJSONObject("duration");
int value = duration.getInt("value");
}
}
The code above has produced following output using your json String: 14147
P.S. You can make use of a library you wish. This one used here was purposed for the demonstrating.
get value by key jsonarray
You can parse your jsonResponse
like below code :
private void parseJsonData(String jsonResponse){
try
{
JSONArray jsonArray = new JSONArray(jsonResponse);
for(int i=0;i<jsonArray.length();i++)
{
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
String value1 = jsonObject1.optString("key1");
String value2 = jsonObject1.optString("key2");
String value3 = jsonObject1.optString("key3");
String value4 = jsonObject1.optString("key4");
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
Access only the First Object from a JSON Array
The data at "1" refers to another JSONArray
, so to get the first item in the array you would do something like this:
JSONArray array = contact.getJSONArray("1");
array.getString(0);
In JSON objects are denoted using {}
and arrays with []
. If you're trying to access a specific value in your JSON object then it's really just a case of following the path to it, through the arrays and objects.
how to access json array element in java
EDIT
Try following:
String response=response.getBody();
JSONObject res = new JSONObject();
System.out.println(res);
int len=res.size();
System.out.println(len);
JSONParser parser=new JSONParser();
Object obj = parser.parse(response);
JSONArray array = (JSONArray)obj;
res=(JSONObject)array.get(0);
System.out.println(res.get("bId"));
Output :
n86nbnhbnghgy76
This one is based on your code and with Simple Json Library
.
How to access repeated elements in JSON object java?
You need first to get the "elements" property as a JSONArray. Then you can retrieve the JSONObject holding lat as follow:
JSONObject jsonObj = new JSONObject(string);
JSONArray resultArray = jsonObj.getJSONArray("elements");
for (int i = 0; i < resultArray.length(); i++) {
JSONObject item = resultArray.getJSONObject(i);
// Now you can retrieve the lat
double lat = item.getDouble("lat"); // Not getString("lat") because lat is a number, not a string
...
}
Java - How to get object value in JSON array?
JSON needs to be traversed in order to access id
:
JSONArray results = shipmentData.getJSONArray("results");
JSONObject first = results.getJSONObject(0);
JSONObject shipper = first.getJSONObject("shipper");
Integer id = shipper.getInt("id");
Parse int to string:
String id = String.valueOf(shipper.getInt("id"));
Getting JSONObject from JSONArray
JSONArray objects have a function getJSONObject(int index)
, you can loop through all of the JSONObjects by writing a simple for-loop:
JSONArray array;
for(int n = 0; n < array.length(); n++)
{
JSONObject object = array.getJSONObject(n);
// do some stuff....
}
Related Topics
Look and Feel Is Not Updating in Swing Jtabbedpane
Why Is Spring's Applicationcontext.Getbean Considered Bad
How to Monitor the Computer's Cpu, Memory, and Disk Usage in Java
How to Extract a Substring Using Regex
What Is the "Execute Around" Idiom
Why Use a Prime Number in Hashcode
Avoid Jackson Serialization on Non Fetched Lazy Objects
Http Servlet Request Lose Params from Post Body After Read It Once
Static Method in a Generic Class
Why Doesn't Java Send the Client Certificate During Ssl Handshake
Java Securityexception: Signer Information Does Not Match
Collect Successive Pairs from a Stream
Struts2 Input Result: How Does It Work? How Are Conversion/Validation Errors Handled
Using Prepared Statements to Set Table Name