getJsonObject in JsonObject cannot be applied to int - android

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();
}

Related

How to get 2 json objects at once?

I'm using volley to get json object, I'm getting data like this
{"resultUser":19}
{"resultUser2":13}
How to get either the second one (resultuser2) or both?
try {
JSONObject o = new JSONObject(response);
String data = (String) o.get("resultUser2");
if (!data.equals("")) {
Toast.makeText(getApplicationContext(), "user2 id" + data, Toast.LENGTH_LONG).show();
//UserDetailsActivty.this.finish();
} else {
Toast.makeText(getApplicationContext(), "Ohh! Sorry,,Signing Up Failed ", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
you can use JsonArray to iterate through all the jsonobjects like this
try {
JSONArray jsonArray = new JSONArray(response);
for (int i =0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.get(i);
//do whatever you want to do with the data
}
} catch(Exception e) {
}

how to add all values of JSONObject to JSONArray

First, I want to add all ArrayList Values into JsonObject then I want to add these JSON object to JsonArray I have done wright in the first I have added all ArrayList to JsonObject but I can't add all JsonObject to JsonArray.
This is my code to convert json object to json Array:-
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
for (int i = 0; i < records.size(); i++) {
try {
jsonObject.put("sid", records.get(i).getsid());
jsonObject.put("name", records.get(i).getpName());
jsonArray.put(jsonObject));
} catch (JSONException e) {
e.printStackTrace();
}
}
Log.e("ATTENDANCE", jsonArray.toString() + "");
}
});
I am getting only the last value of the ArrayList please help me I want The output to be like these
[
{
"student_no":"3924/08",
"firstname":"olana
},
{
"student_no":"4011/08",
"firstname":"yallem"
}
]
That is because you're creating only one object and putting it inside the for loop and you have to create new objects for every iteration.
Put your
JSONObject jsonObject = new JSONObject();
inside your for loop
like this
for (int i = 0; i < records.size(); i++) {
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("sid", records.get(i).getsid());
jsonObject.put("name", records.get(i).getpName());
jsonArray.put(jsonObject));
} catch (JSONException e) {
e.printStackTrace();
}
}
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < records.size(); i++) {
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("sid", records.get(i).getsid());
jsonObject.put("name", records.get(i).getpName());
jsonArray.put(jsonObject));
} catch (JSONException e) {
e.printStackTrace();
}
}
Log.e("ATTENDANCE", jsonArray.toString() + "");
}
});

How to parse a JSONArray and assign it to different TextViews?

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();
}

How to put json array in json object

This is code which I'm using:
#Override
protected String doInBackground(String... arg0) {
String yourJsonStringUrl = "GetCalender_Events";
JsonParser jParser = new JsonParser();
json = jParser.getJSONFromUrlArray(yourJsonStringUrl);
return null;
}
#Override
protected void onPostExecute(String strFromDoInBg) {
for (int i = 0; i < json.length(); i++) {
try {
JSONObject c = json.getJSONObject(i);
ldatosAgenda.add(new DatosAgenda(c.getString("Event_Name"), c.getString("Event_Name"),
sdf5.format(sdf1.parse(c.getString("Column1"))), sdf6.format(sdf1.parse(c.getString("Column1"))),
c.getString("Description")));
} catch (JSONException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
How to put my JSON Array into JSON object? Can anyone please help me, this is my first try with JSON Array.
My array looks like this:
{
"ContactList": [
{
"Column1": "22-05-2017",
"Event_Name": "Garba Compition",
"Description": "School organized garba compition"
},
{
"Column1": "24-05-2017",
"Event_Name": "Mahendi Compition",
"Description": "Mahendi compition"
}
]
}
#. Seems you are getting JSONObject as response. So you have to parse JSONObject first from sever response:
JSONObject json = jParser.getJSONFromUrlObject(yourJsonStringUrl);
#. Then parse ContactList and its item as below:
#Override
protected void onPostExecute(String strFromDoInBg) {
JSONArray jsonArray = json.getJSONArray("ContactList");
for (int i = 0; i < jsonArray.length(); i++) {
try
{
JSONObject c = jsonArray.getJSONObject(i);
ldatosAgenda.add(new DatosAgenda(c.getString("Event_Name"),c.getString("Event_Name"), sdf5.format(sdf1.parse(c.getString("Column1"))), sdf6.format(sdf1.parse(c.getString("Column1"))),c.getString("Description")));
} catch (JSONException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
JSONArray jsonarray = new JSONArray();
JSONObject jsonObject = new JSONObject();
try{
jsonObject.put(jsonarray);
} catch(JSONException e){
e.printStackTrace();
}
You get the values
JSONArray songobj = json.getJSONArray("ContactList");
Log.d(TAG, "List Length" + songobj.length());
for (int i = 0; i < songobj.length(); i++) {
JSONObject song = songobj.getJSONObject(i);
String column=song.getString("Column1");
String name=song.getString("Event_Name");
String desc=song.getString("Description");
ldatosAgenda.add(new DatosAgenda(column,name,desc));
}
}

deleting value inside JsonArray

I wanna delete the value inside this jsonArray:
{"category_ids":[0,1,2,3,4,5],"keyword":""}
For example, i wanna remove 0 from category_ids jsonArray.
Can anybody help me? Thanks in advance.
String load = "{\"category_ids\":[0,1,2,3,4,5],\"keyword\":\"\"}";
try {
JSONObject jsonObject_load = new JSONObject(load);
JSONArray jsonArray = jsonObject_load.getJSONArray("category_ids");
Log.d("out", RemoveJSONArray(jsonArray,1).toString());
} catch (JSONException e) {
e.printStackTrace();
}
public static JSONArray RemoveJSONArray( JSONArray jarray,int pos) {
JSONArray Njarray = new JSONArray();
try {
for (int i = 0; i < jarray.length(); i++) {
if (i != pos)
Njarray.put(jarray.get(i));
}
} catch (Exception e) {
e.printStackTrace();
}
return Njarray;
}
Below API 19
String result = "{\"category_ids\":[0,1,2,3,4,5],\"keyword\":\"\"}";
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray("category_ids");
jsonArray = removeValue(jsonArray, 0); // below api 19
// jsonArray.remove(0); // above api 19
} catch (JSONException e) {
e.printStackTrace();
}
public JSONArray removeValue(JSONArray jsonArray, int index) {
JSONArray copyArray = new JSONArray();
try {
if (index < jsonArray.length()) {
for (int i = 0; i < jsonArray.length(); i++) {
if (i != index) {
copyArray.put(jsonArray.get(i));
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return copyArray;
}
Above API 19 use jsonArray.remove(0);
I hope this will help you.

Categories

Resources