i am trying to make an app that requires to loop through an array of JSON objects. For this i first create an ArrayList and then convert it to an Array and loop through it, however i get the following error:
Day::drawEvents |
StringIndexOutOfBoundsException: length = 16;
regionStart = -2;
regionLength = 1
The error doesn't even show up on the logcat as an error it only displays as popup message on the device.
The code that i suspect causes the error:
List<JSONObject> appointments = new ArrayList<JSONObject>();
JSONObject jsonObject = new JSONObject(jsonAppointments);
JSONObject jsonResponse = jsonObject.getJSONObject("response");
JSONArray jsonData = jsonResponse.getJSONArray("data");
for (int i = 0; i < jsonData.length(); i++) {
appointments.add(jsonData.getJSONObject(i));
}
List<JSONObject> appointments = new ArrayList<JSONObject>();
...adding items to the ArrayList...
JSONObject[] appointmentsArray = new JSONObject[appointments.size()];
appointments.toArray(appointmentsArray);
int len = appointments.size();
for (int i = 0; i < len; i++) {
start = appointmentsArray[i].getInt("start") * 1000L;
...processing the array content...
}
Related
Here is my code for storing multiple JSON objects into an array and then I want to convert the array into a single string.
String message =null;
JSONObject json = new JSONObject();
json.put("name","student");
json.put("name", "Atal");
json.put("name","Rachit");
JSONArray array = new JSONArray();
for(int i= 0; i<= array.length(); i++) {
array.put(json);
message = array.toJSONString();
}
I want to get the output as a string and store it in a message variable.
String message =null;
JSONObject json = new JSONObject();
json.put("name","student");
json.put("name", "Atal");
json.put("name","Rachit");
JSONArray array = new JSONArray();
//for(int i= 0; i<= array.length(); i++) {
// array.put(json);
// message = array.toJSONString();
//}
array.put(json); //There is a JSON,just put it into the array
message = array.toJSONString();
I was replacing "name" every time I was calling json.put()
String message =null;
JSONObject json = new JSONObject();
json.put("name","Atal");
json.put("class", "10");
json.put("Roll","1035");
JSONArray array = new JSONArray();
array.put(json);
message = array.toJSONString();
How do I get a string from Parse JSON with data like below?
My Data JSON :
[
[
"John Gorman",
"3344764667200003",
"Student",
"2018-08-09 08:21:30.807"
],
[
"Andy William",
"3403032311690003",
"Student",
"2018-08-09 08:21:30.807"
],
[
"Thomas Endry",
"3408932311690078",
"Student",
"2018-08-09 08:21:30.807"
],
[
"Robet Calm",
"3403077711690890",
"Student",
"2018-08-09 08:21:30.807"
]
]
I use the code below, then to get the string what I have to do, I really don't understand, if there is help, I really need to thank you.
try {
JSONObject object = new JSONObject(result);
JSONArray jsonArray = object.getJSONArray("");
//JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
String name = c.getString("What should I write here");
System.out.println(c.getString(""));
}
I want to get the string from Parse JSON that I have
How do I get a string for listview, for example:
John Gorman
3344764667200003
Student
Andy William
3403032311690003
Student
Thomas Endry
3408932311690078
Student
Robet Calm
3403077711690890
Student
A couple of things are wrong with the posted code. See my comments inline:
JSONObject object = new JSONObject(result);
^^^^^^^^^^^^^^^^^^^^^^
the content of result is not an object, but an array!
JSONArray jsonArray = object.getJSONArray("");
^^
a proper JSON document had better not have empty keys!
//JSONArray jsonArray = new JSONArray(result);
^^^^^^^^^^^^^^^^^^^^^
this was actually going in the right direction!
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
^^^^^^^^^^^^^^^^^^^^^^^^^^
the i-th element of the array is not an object, but an array!
Fixing the issues above:
JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONArray subArray = jsonArray.getJSONArray(i);
for (int j = 0; j < subArray.length(); j++) {
System.out.println(subArray.getString(j));
}
System.out.println();
}
You are receiving a JSONArray not object so use the below code.
for(int i=0;i<jsonarray.length();i++){
JSONArray array=jsonarray.getJSONArray(i);
for(int j=0;j<array.length();j++){
Log.d(TAG,array.getString(j));
}
}
You can do something like this:
try {
JSONObject object = new JSONObject(result);
JSONArray jsonArray = object.getJSONArray("");
//JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
String name = c.getString(0);
String id = c.getString(1);
String student = c.getString(2);
// and so on ... This index refers to the index number on the string inside json object
System.out.println(name);
}
Hope this answers your query.
Following method will construct a list of string from the given Json. You can pass that list in the adapter of the list view.
public List<String> getList (String result) throws JSONException {
List<String> list = new LinkedList<>();
JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONArray arr = jsonArray.getJSONArray(i);
StringBuilder sb = new StringBuilder();
for(int j=0;j<arr.length(); j++) {
sb.append(arr.getString(j));
if(j<arr.length()-1) sb.append("\n");
}
list.add(sb.toString());
}
return list;
}
Output of String return: [{"type":1, "txt":"ERROR"}]. I'm trying to get the content of txt key which is ERROR. And that
by transforming string return into an array. However I'm getting some errors commented next to each line on the follow code.
Any insight? I tried everything to retrieve the value of txt.
Vector<ClsReturn> ret = null;
ret = ds.id(collection, "fs",in_uri );
String return = JSONArray.toJSONString(ret);
JSONObject myJsonObject = new JSONObject(ret);
JSONArray array = new JSONArray(return); //The constructor JSONArray(String) is undefined
for(int i=0; i<array.length(); i++){ //The method length() is undefined for the type JSONArray
JSONObject jsonObj = array.getJSONObject(i); //The method getJSONObject(int) is undefined for the type JSONArray
System.out.println(jsonObj.getString("txt"));
}
Try this
String retur = JSONArray.toJSONString(ret);
JSONObject myJsonObject = new JSONObject();
JSONArray array = new JSONArray(retur);
int i = 0;
while(i < array .length()){
myJsonObject = array .getJSONObject(i);
System.out.println(myJsonObject.getString("txt"));
i++;
}
Im having issues with being able to get the indivisual responses from this JSON Array. Im trying to write some code that will allow me to use the vaules indivisually, as well as how to combine them if i needed too. These responses are always integers.
Java
JSONObject jsonObj = new JSONObject(jsonStr);
data_array = jsonObj.getJSONArray("data");
for (int i = 0; i < data_array.length(); i++) {
JSONObject prod = data_array.getJSONObject(i);
Log.d("Logging Response", prod);
}
Json
[5652,8388,8388,7537,8843,2039,8235,0,12220]
I'd like to figure out how i can add them together and return the calculated result, as well as how to use them individually. such as (prod + prod + prod) etc...
JSONArray data = jsonObj.getJSONArray("data");
for (int i = 0; i < data.length(); i++) {
Integer prod = (Integer) data.get(i);
System.out.println("Prod " + prod);
// loop and add it to array or arraylist
}
You have the jsonarray as the response
ArrayList<Integer> jsonvalue=new ArrayList<Integer>();
for (int i = 0; i < data_array.length(); i++) {
int i= data_array.getInt(i);
jsonValue.add(i)
}
To put that json array data in to a new json object, try this:
JSONObject jsonObj = new JSONObject(jsonStr);
data_array = jsonObj.getJSONArray("data");
JSONObject prod;
for (int i = 0; i < data_array.length(); i++) {
prod.put(i,data_array.getJSONObject(i));
Log.d("Logging Response", prod);
}
JSONArray jsonObj = new JSONArray(jsonStr);
for (int i = 0; i < jsonObj.length(); i++) {
System.out.println(jsonObj.getString(i));
}
Forgive for I am new to Android and a novice at Java. I am trying to create a dynamic list view using data from a MySQL server. However, sometimes a query returns only one result. When my adapter class parses a JSONArray with one element to a String array, I receive an ArrayIndexOutOfBoundsException. How do I avoid using an empty string in my resultArray to compensate for the exception? I don't want to use the empty string because it will still be selectable within the listview.
Parsing code:
try
{
jArray = new JSONArray(result);
// Taking a peek at the contents
Log.e("log_tag", jArray.getJSONObject(0).getString(queryID));
//For some reason if I don't do this
// I get ArrayIndexOutOfBoundsException.
if (jArray.length() == 1)
{
resultArray = new String[2];
resultArray[1] = "";
}
else
resultArray = new String[jArray.length()];
for(int i = 0; i < jArray.length(); i++)
{
resultArray[i] = jArray.getJSONObject(i).getString(queryID);
}
}
catch(JSONException e)
{
throw new NullResultFromServerException("No results from server.");
}
for(int i = 0; i < jArray.length(); i++)
{
String empty= jArray.getJSONObject(i).getString(queryID);
empty=empty.trim(); //this will remove the blank white space
if(!empty.equalsIgnoreCase(""))
resultArray[i] = empty;
}