I have a JSONArray and when I try to parse it, an NPE shows and the logcat shows W/System.err: org.json.JSONException: Value at 0 is null. Please help. I have provided my codes below.
JSON Array
[
{
"student_number":"201411870",
"full_name":"Miranda , Andrew Matthew Matera",
"year":"4",
"course":"BSIT"
}
]
Code Snippet
ArrayList<User> userArrayList = new JsonConverter<User>().toArrayList(response, User.class);
JSONArray jsonArray = new JSONArray(userArrayList);
try {
int regStudentNumber = jsonArray.getJSONObject(0).getInt("student_number");
String regFullName = jsonArray.getJSONObject(1).getString("full_name");
int regYear = jsonArray.getJSONObject(2).getInt("year");
String regCourse = jsonArray.getJSONObject(3).getString("course");
tvStudentNumber.setText(String.valueOf(regStudentNumber));
tvFullName.setText(regFullName);
tvYear.setText(String.valueOf(regYear));
tvCourse.setText(regCourse);
} catch (JSONException e) {
e.printStackTrace();
}
Solved it on my own. Sorry for the unclear question.
try {
JSONArray jsonArray = new JSONArray(response);
if(jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
int regStudentNumber = jsonObject.getInt("student_number");
String regFullName = jsonObject.getString("full_name");
int regYear = jsonObject.getInt("year");
String regCourse = jsonObject.getString("course");
tvStudentNumber.setText(String.valueOf(regStudentNumber));
tvFullName.setText(regFullName);
tvYear.setText(String.valueOf(regYear));
tvCourse.setText(regCourse);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
First of all parse JSON array to object to make clear it is for a particular value then fetch things from it based on need.
To parse json array to object use code
try {
JSONArray jsonArray = new JSONArray(response);
if(jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Related
I'm getting the above-mentioned error while trying to extract data from JSONObject.My objective is to extract the "transactionId" data and store it in a variable for future use.
What I have so far:
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (pd.isShowing()) {
pd.dismiss();
}
/*txtJson.setText(result);*/
JSONObject jsonarray;
try {
jsonarray = new JSONObject(result);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject mJsonObject = jsonarray.getJSONObject(i);
Log.d("OutPut", mJsonObject.getString("transactionId"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
My JSON Object is shown below:
{
"merchantId":"X",
"transactionId":"Y"
}
I'm new to Programming so any help would be appreciated.
Try this code
JSONArray jsonarray;
try {
jsonarray = new JSONArray(result);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject mJsonObject = jsonarray.optJSONObject(i);
Log.d("OutPut", mJsonObject.optString("transactionId"));
}
} catch (JSONException e) {
e.printStackTrace();
}
Also, if there is single ITEM in your JSONArray, you can remove forLoop.
And, if the response received is JSONObject then,
JSONObject jsonObject;
try {
jsonObject = new JSONObject(result);
Log.d("OutPut", jsonObject.optString("transactionId"));
} catch (JSONException e) {
e.printStackTrace();
}
I want to get the id ^& content value in http://rest-service.guides.spring.io/greeting
What i tried is,
try {
JSONObject jsonObj = new JSONObject(parsingUrl);
// If you have array
JSONArray resultArray = jsonObj.getJSONArray("id"); // Here you will get the Array
// Iterate the loop
for (int i = 0; i < resultArray.length(); i++) {
// get value with the NODE key
JSONObject obj = resultArray.getJSONObject(i);
String name = obj.getString("content");
}
// If you have object
//String result1 = jsonObj.getString("result");
} catch (Exception e) {
e.printStackTrace();
}
Thanks
The url that your mentioned dont have json arrays, parsing will be like
try {
JSONObject jsonObj = new JSONObject(resultfromUrl);
int id = jsonObj.getInt("id");
String name = jsonObj.getString("content");
} catch (JSONException e) {
e.printStackTrace();
}
In order to combine 2 JSON arrays into one I use the following code (the input data areJSON arrays that wereconverted to a string)
private static String combineData(String Data, String data){
if (empty(data))
data = Data;
else { // need to append the data
try {
// first, convert the strings back to JSON objects
JSONObject jsonObj1 = new JSONObject(data); // existing data
JSONObject jsonObj2 = new JSONObject(Data); // new data
// Getting Array of Providers
String fieldName = context.getString(R.string.JSON_COMP_RECORDS);
// Second, get the array out of the JSON object
JSONArray record1 = jsonObj1.getJSONArray(fieldName);
JSONArray record2 = jsonObj2.getJSONArray(fieldName);
// Third, join them into one array
JSONArray arr = new JSONArray();
// LOOP 1 - get individual JSON objects
for (int i = 0; i < record1.length(); i++) {
JSONObject c;
try {
c = record1.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
return data;
}
arr.put (c);
}
// LOOP 1 - get individual JSON objects
for (int i = 0; i < record2.length(); i++) {
JSONObject c;
try {
c = record2.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
return data;
}
arr.put (c);
}
JSONObject json = new JSONObject();
json.put (context.getString(R.string.JSON_COMP_RECORDS), arr);
data = json.toString();
} catch (JSONException e) {
e.printStackTrace();
}
}
return data;
}
I wonder if there is a way to do it more efficiently and get rid of the 2 loops?
How about this ? You can put JSONObject directly int another array
JSONArray record1 = jsonObj1.getJSONArray(fieldName);
JSONArray record2 = jsonObj2.getJSONArray(fieldName);
for (int i = 0; i < record1.length(); i++) {
JSONObject c;
try {
c = record1.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
return data;
}
record2.put (c);
}
Hope this helps
JSONArray record1 = jsonObj1.getJSONArray(fieldName);
JSONArray record2 = jsonObj2.getJSONArray(fieldName);
for (int i = 0; i < record1.length(); i++) {
record2.put(record1.getJSONObject(i));
}
How can I loop through this kind of JSON array?
[
{
"full_name": "Abc Xyz"
},
{
"full_name": "Def Xyz"
},
{
"full_name": "Nml Xyz"
},
{
"full_name": "Jol Xyz"
}
]
Thank you!
try this
try {
JSONArray a = new JSONArray(myjsonString);
for(int i = 0; i < a.length(); i++)
{
JSONObject o = a.getJSONObject(i);
String name = o.getString("full_name");
}
} catch (JSONException e) {
e.printStackTrace();
}
Your jsonString contains array of objects so here is code:
try {
// [] indicates array so top element is array
JSONArray jsonArray = new JSONArray(jsonString);
for(int i = 0; i < jsonArray.length(); i++)
{
// {} indicates object so array elements are objects
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("full_name");
}
} catch (JSONException e) {
e.printStackTrace();
}
Updated : Google GSON
Also try Google's GSON this is excellent library to handle jsons you can serialize and deserialize the json and class objects.
checkout this link: Google Gson
Try
try {
JSONArray array = new JSONArray(jsonString);
for(int i = 0; i < array.length(); i++) {
JSONObject json = array.getJSONObject(i);
String fullName = json.getString("full_name");
}
} catch (JSONException e) {
e.printStackTrace();
}
I am trying to get a list of available numbers from the following json object, using the class from org.json
{
"response":true,
"state":1,
"data":
{
"CALLERID":"81101099",
"numbers":
[
"21344111","21772917",
"28511113","29274472",
"29843999","29845591",
"30870001","30870089",
"30870090","30870091"
]
}
}
My first steps were, after receiving the json object from the web service:
jsonObj = new JSONObject(response);
jsonData = jsonObj.optJSONObject("data");
Now, how do I save the string array of numbers?
use:
jsonObj = new JSONObject(response);
jsonData = jsonObj.optJSONObject("data");
JSONArray arrJson = jsonData.getJSONArray("numbers");
String[] arr = new String[arrJson.length()];
for(int i = 0; i < arrJson.length(); i++)
arr[i] = arrJson.getString(i);
you need to use JSONArray to pull data in an array
JSONObject jObj= new JSONObject(your_json_response);
JSONArray array = jObj.getJSONArray("data");
Assuming that you are trying to get it in a javascript block, Try something like this
var arrNumber = jsonData.numbers;
My code is for getting "data":
public void jsonParserArray(String json) {
String [] resultsNumbers = new String[100];
try {
JSONObject jsonObjectGetData = new JSONObject(json);
JSONObject jsonObjectGetNumbers = jsonObjectGetData.optJSONObject("results");
JSONArray jsonArray = jsonObjectGetNumbers.getJSONArray("numbers");
for (int i = 0; i < jsonArray.length(); i++) {
resultsNumbers[i] = jsonArray.getString(i);
}
} catch (JSONException e) {
e.printStackTrace();
Log.e(LOG_TAG, e.toString());
}
}