this is the response from the server
{"route":[1,2,3,4,5,6]}
This is my code:
try{
String d = json.getString("route");
}
}catch(JSONException je){
}
and im getting NullPointerException.
please help me.
This is my Server Response, Now Give me the solution
Link -> http://ajax.tpksym.cloudbees.net/route/route14
It should be like:
JSONObject jsonResult = new JSONObject("{\"route\":[1,2,3,4,5,6]}");
JSONArray array = jsonResult.getJSONArray("route");
for (int i = 0; i < array.length(); i++) {
int data = array.getInt(i);
}
....
try {
String jsonString = "{\"route\":[1,2,3,4,5,6]}";
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray("route");
for (int i = 0; i < jsonArray.length(); i++) {
System.out.println(jsonArray.getInt(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
Hi as yours json is as follows
{
"route": [
1,
2,
3,
4,
5,
6
]
}
so do as follows
String jsondata = "{\"route\":[1,2,3,4,5,6]}";
JSONObject primaryObject = new JSONObject(jsondata);
JSONArray jarray = primaryObject.getJSONArray("route");
for (int i = 0; i < jarray.length(); i++) {
Integer data = jarray.getInt(i);
System.out.println("data=="+data);
}
as you gave the link http://ajax.tpksym.cloudbees.net/route/route14
and data seems there coming as in double i.e. 13.56 etc
so use as follows
String jsondata = "JSON DATA FROM SERVER";
JSONObject primaryObject = new JSONObject(jsondata);
JSONArray jarray = primaryObject.getJSONArray("route");
for (int i = 0; i < jarray.length(); i++) {
Double data = jarray.getDouble(i);
}
Related
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;
}
I'm trying to save json_data from server to sqlite, but i'm getting the error
Value Next of type java.lang.String cannot be converted to JSONArray
Here is my json : My Json
And here is my java code :
String json_data = stringBuilder.toString().trim();
JSONObject object = new JSONObject(json_data);
JSONObject object1 = new JSONObject("Next");
JSONArray Jarray = object1.getJSONArray("Days");
for (int i = 0; i < Jarray.length(); i++)
{
JSONObject Jasonobject = Jarray.getJSONObject(i);
dbHelper.putStationsInformation((Jasonobject),sqLiteDatabase);
}
dbHelper.close();
I know this code looks weird but please help me do this.
You need to fetch Next jsonobject instead of creating it here with a single String like
new JSONObject("Next"); , so fetch it from response object like
JSONObject object1 = object.getJSONObject("Next");
"Value Next of type java.lang.String cannot be converted to
JSONArray".
Rectify getJSONObject section.
Dont
JSONObject object1 = new JSONObject("Next");
Do
JSONObject obj = new JSONObject(json_data );
JSONObject _jsonOBJ = obj.getJSONObject("Next");
JSONArray Jarray = _jsonOBJ .getJSONArray("Days");
for (int i = 0; i < Jarray.length(); i++)
{
JSONObject Jasonobject = Jarray.getJSONObject(i);
dbHelper.putStationsInformation((Jasonobject),sqLiteDatabase);
}
JSONObject result_obj= new JSONObject(result.toString());
JSONObject result_array= result_obj.getJSONObject("Next");
JSONArray array_values = result_array.getJSONArray("Days");
for (int i = 0; i < Jarray.length(); i++)
{
JSONObject plan = Jarray.getJSONObject(i);
plan.getString("Day");
for(int j=0;j<plan.lenght();j++){
}
}
You need to Parse the "Next" object from the json provided. You have created a new JSONObject that was the mistake.
try {
String json_data = stringBuilder.toString().trim();
JSONObject object = new JSONObject(json_data);
JSONObject object1 =object.getJSONObject("Next");
JSONArray Jarray = object1.getJSONArray("Days");
for (int i = 0; i < Jarray.length(); i++)
{
JSONObject Jasonobject = Jarray.getJSONObject(i);
dbHelper.putStationsInformation((Jasonobject),sqLiteDatabase);
dbHelper.close();
}
} catch (JSONException e) {
e.printStackTrace();
}
Happy coding!!
You have created a new JSONObject("Next")which means you are converting a string "Next" into a JSONObject.
Rectify to this:
String json_data = stringBuilder.toString().trim();
JSONObject object = new JSONObject(json_data);
JSONObject object1 = object.getJSONObject("Next");
I have code like this, the value of jArrAnswer is
[{"answer":"Yes"},{"answer":"No"},{"answer":"maybe"},{"answer":"yrg"}]
the result from jArrAnswer.length() is 4
but why I got error
org.json.JSONException: Index 1 out of range [0..1).
try {
JSONArray jArrAnswerid = new JSONArray(answerid);
JSONArray jArrAnswer = new JSONArray(answer);
for (int i = 0; i < jArrAnswer.length(); i++) {
JSONObject jObjAnswerid = jArrAnswerid.getJSONObject(i);
JSONObject jObjAnswer = jArrAnswer.getJSONObject(i);
String ansid = jObjAnswerid.getString("answerid");
String ans= jObjAnswer.getString("answer");
GroupModel item2 = new GroupModel(String.valueOf(i + 1), ans, ansid);
}
} catch (Exception e) {
Log.w("asdf", e.toString());
}
You are iterating the for loop over jArrAnswer while your fetching the index i over jArrAnswerid.
Check and make sure that the jArrAnswerid.size() is equal to the jArrAnswer.size().
Print the jArrAnswerid.size() and check.
Try this
try {
JSONArray jArrAnswer = new JSONArray(answer);
for (int i = 0; i < jArrAnswer.length(); i++) {
JSONObject jObjAnswer = jArrAnswer.getJSONObject(i);
String ansid = jObjAnswer.getString("answerid");
String ans= jObjAnswer.getString("answer");
}
} catch (Exception e) {
Log.w("asdf", e.toString());
}
provided "answer" is your json array response
Try
String json = "[{\"answer\":\"Yes\",\"answerid\":\"1\"},{\"answer\":\"No\",\"answerid\":\"2\"},{\"answer\":\"maybe\",\"answerid\":\"3\"},{\"answer\":\"yrg\",\"answerid\":\"4\"}]";
try {
JSONArray jsonArray = new JSONArray(json);
if(jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String answerId = jsonObject.getString("answerid");
String answer = jsonObject.getString("answer");
//Use answerId and answer
}
}
} catch(JSONException e) {
e.printStackTrace();
}
I am facing this exception at 0 of type java.lang.String cannot be converted to JSONArray don't know why? i have use this before in different work didn't get this exception
List<NameValuePair> pair = new ArrayList<>();
pair.add(new BasicNameValuePair("id", String.valueOf(getitemno)));
json = JSONParser.makeHttpRequest("http://192.168.1.51:80/StopViewApi/index.php","POST",pair);
Log.d("Route Response", json.toString());
int success = 0;
try {
success = json.getInt(TAG_SUCCESS);
Routearrray = new ArrayList<String>();
} catch (JSONException e) {
e.printStackTrace();
}
try {
if (success == 1) {
JSONArray jsonarray;
json = new JSONObject(JSONParser.Result);
JSONArray jArray = json.getJSONArray("data");
for (int i = 0; i <= jArray.length(); i++) {
jsonarray = jArray.getJSONArray(i);
Stopnames = jsonarray.getString(0);
Counter++;
if (Counter <=jArray.length()) {
//Routearrray.add(Stopnames);
Log.d("Stop name:", Stopnames);
//Log.d("Route name:", Routearrray.toString());
} else {
break;
}
}
JSON URL
{
"data":[
"Rawat","Islamabad Mor","Kaak Pull","Lohi Bher","Koral Chowk","Gangal",
"Khana Bridge","Zia Masjid","Kuri Road","Dhok Kala Khan","Faizabad",
"Pirwadhai Mor","Tanki Stop I-8\/4","I-8\/3 Stop","Al Shifa Hospital","AIOU",
"Zero Point","Children Hospital","F-8\/4","Ali Hospital"
],
"success":1,
"status":200,
"status_message":"Login Successfull"
}
Error: at Line
jsonarray = jArray.getJSONArray(i);
From your JSON, jArray is a array of String and not an array of JSONArray.
Replace:
jsonarray = jArray.getJSONArray(i);
Stopnames = jsonarray.getString(0);
with:
Stopnames = jArray.getString(i);
the for loop should be
for (int i = 0; i < jArray.length(); i++) {
You're trying to parse String values as JSONArray. jArray is an array of Strings, not JSONArrays, so all you need to do is just extract the String values from it.
JSONArray jArray = json.getJSONArray("data");
// You already have a JSONArray, now all you need to do is extract String values from it
for (int i = 0; i <= jArray.length(); i++) {
Log.d("Stopnames: " , jArray.getString(i));
....
}
Thats because you "data" is of type json array. whats inside "data" is not a jsonarray, they are just jsonstrings.
change the following lines
jsonarray = jArray.getJSONArray(i);
Stopnames = jsonarray.getString(0);
to
stopnames += jArray.getString(i);
You are trying to get jsonarray from jsonarray, while the json array contains json objects. try this
JSONObject jsonObject = jsonArray.getJSONObject(i)
Stopnames = jsonObject.getString(Integer.toString(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));
}