JSON parsing to List view with multiple objects Android [closed] - android

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Need Help
I have Json through which i have to retrieve objects but unable to retrieve.
I have used multiple objects also to retrieve but no success.
JSON:
{
"status": 1,
"data": [{
"restaurent_id": "1",
"user_id": "6",
"zone_id": "1",
"restaurentAddress": {
"restaurent_address_id": "1"
},
"restaurentInfo": {
"restaurent_info_id": "1",
"restaurent_bussiness_owner_name": "Vijay"
},
"restaurentSetting": {
"restaurent_setting_id": "1",
"minimum_purcase": "200",
"payment_method_id": "1",
"title": "Best Hotel"
},
"zone": {
"zone_id": "1",
"by_zipcode": "1"
}
}]
}
and i want to fetch restaurentAddress and restaurentInfo
MY mainActivity.java file
package com.example.premi.jsonlist;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView output = (TextView) findViewById(R.id.textView1);
String strJson="{\n" +
"\t\"status\": 1,\n" +
"\t\"data\": [{\n" +
"\t\t\"restaurent_id\": \"1\",\n" +
"\t\t\"user_id\": \"6\",\n" +
"\t\t\"zone_id\": \"1\",\n" +
"\t\t\"restaurentAddress\": {\n" +
"\t\t\t\"restaurent_address_id\": \"1\"\n" +
"\t\t},\n" +
"\t\t\"restaurentInfo\": {\n" +
"\t\t\t\"restaurent_info_id\": \"1\",\n" +
"\t\t\t\"restaurent_bussiness_owner_name\": \"Vijay\"\n" +
"\t\t},\n" +
"\t\t\"restaurentSetting\": {\n" +
"\t\t\t\"restaurent_setting_id\": \"1\",\n" +
"\t\t\t\"minimum_purcase\": \"200\",\n" +
"\t\t\t\"payment_method_id\": \"1\",\n" +
"\t\t\t\"title\": \"Best Hotel\"\n" +
"\t\t},\n" +
"\t\t\"zone\": {\n" +
"\t\t\t\"zone_id\": \"1\",\n" +
"\t\t\t\"by_zipcode\": \"1\"\n" +
"\t\t}\n" +
"\n" +
"\t}]\n" +
"}";
String dataoutput = "";
try {
JSONObject jsonRootObject = new JSONObject(strJson);
//Get the instance of JSONArray that contains JSONObjects
JSONObject status = jsonRootObject.optJSONObject("status");
JSONArray Dataarray =status.getJSONArray("data");
//Iterate the jsonArray and print the info of JSONObjects
for(int i=0; i < Dataarray.length(); i++){
JSONObject jsonObject = Dataarray.getJSONObject(i);
JSONObject Data = jsonObject.getJSONObject("restaurentAddress");
for(int j = 0 ; j < Data.length(); j++){
JSONObject GetData =Data.getJSONObject(String.valueOf(j));
int id = Integer.parseInt(GetData.getString("restaurent_address_id"));
String postcode = GetData.getString("postcode");
String addresss = GetData.getString("restaurent_address");
dataoutput += " : \n id= "+ id +" \n postcode= "+ postcode +" \n address= "+ addresss +" \n ";
}}
output.setText(dataoutput);
} catch (JSONException e) {e.printStackTrace();}
}
}

Try this out:
try {
JSONObject object = (JSONObject) new JSONTokener(YOUR_JSON_STRING).nextValue();
String restaurentAddressId = object.getJSONArray("data").getJSONObject(0).getJSONObject("restaurentAddress").getString("restaurent_address_id");
String restaurentInfoId = object.getJSONArray("data").getJSONObject(1).getJSONObject("restaurentInfo").getString("restaurent_info_id");
String restaurentBizOwnerName = object.getJSONArray("data").getJSONObject(1).getJSONObject("restaurentInfo").getString("restaurent_business_owner_name");
}
catch (JSONException e) {
}

Its Done I tried this
try {
JSONObject jsonRootObject = new JSONObject(strJson);
//Get the instance of JSONArray that contains JSONObjects
JSONArray mainnode =jsonRootObject.getJSONArray("data");
//Iterate the jsonArray and print the info of JSONObjects
for(int i=0; i < mainnode.length(); i++){
JSONObject jsonObject = mainnode.getJSONObject(i);
JSONObject Data = jsonObject.getJSONObject("restaurentInfo");
int id = Integer.parseInt(Data.getString("restaurent_info_id"));
String postcode = Data.getString("restaurent_phone_number");
String addresss = Data.getString("restaurent_bussiness_owner_name");
dataoutput += " : \n restaurent_id= "+ id +" \n restaurent_info_id= "+ postcode +" \n restaurent_address_id= "+ addresss +" \n ";
}
output.setText(dataoutput);
} catch (JSONException e) {e.printStackTrace();}
}
I just removed Status Object
and another for loop Thanks For the Help Got little Bit Help from You :)

Related

How to parse two string into one textview

I want my TextView contain 2 String of JSON data.
example.json
{
"volumeInfo": {
"title": "Computer Architecture",
"subTitle": "A Quantitative Approach"
}
So, from that JSON i want my TextView to be: Computer Architecture: A Quantitative Approach. How should i do? Thanks.
I only want using one TextView
Just do it
String title = "Computer Architecture";
String subtitle = "A Quantitative Approach";
TextView.setText(title + ": " + subtitle);
You can concatenate Strings and then set it to TextView like below:
String title = "Computer Architecture";
String subtitle = "A Quantitative Approach";
yourTextView.setText(title + "," + subtitle);
//Please check the below code will help you as you want to set the text.
String response = "{" +
" \"volumeInfo\": {" +
" \"title\": \"Computer Architecture\"," +
" \"subTitle\": \"A Quantitative Approach\"}" +
"}";
try {
String title = "", subTitle = "";
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.has("volumeInfo")) {
JSONObject jsonObject1 = jsonObject.getJSONObject("volumeInfo");
if (jsonObject1.has("title")) {
title = jsonObject1.getString("title");
}
if (jsonObject1.has("subTitle")) {
subTitle = jsonObject1.getString("subTitle");
}
String combineString = title + ": " + subTitle;
TextView textView = findViewById(R.id.textView);
textView.setText(combineString);
}
} catch (JSONException e) {
e.printStackTrace();
}

How to get data from key of a JSON [duplicate]

This question already has answers here:
How to iterate over a JSONObject?
(15 answers)
Closed 5 years ago.
I am creating an app in which i need to parse a list of contacts which is in JSONObject format, with key before each object, i don't know how to parse this format.
{
"1": {
"mobileContact": "98562325",
"systemContact": "9198562325"
},
"3": {
"mobileContact": "987563656",
"systemContact": "91987563656"
},
"4": {
"mobileContact": "965632525",
"systemContact": "91965632525"
},
"6": {
"mobileContact": "965436222",
"systemContact": "91965436222"
}
}
Use the keys() iterator to iterate over all the properties, and call get() for each.
Iterator<String> iter = json.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
Object value = json.get(key);
} catch (JSONException e) {
// Something went wrong!
}
}
try{
JSONObject json = new JSONObject(jsonRespondeString);
Iterator<String> iterator = json.keys();
while (iterator.hasNext()){
String key = iterator.next();
JSONObject object = json.getJSONObject(key);
String value1 = object.getString("key1");
String value2 = object.getString("key2");
}
}
catch (JSONException e){
e.printStackTrace();
}
please try this it helps
You can use GSON library to parse it.
String data = "{\n" +
" \"1\": {\n" +
" \"mobileContact\": \"98562325\",\n" +
" \"systemContact\": \"9198562325\"\n" +
" },\n" +
" \"3\": {\n" +
" \"mobileContact\": \"987563656\",\n" +
" \"systemContact\": \"91987563656\"\n" +
" },\n" +
" \"4\": {\n" +
" \"mobileContact\": \"965632525\",\n" +
" \"systemContact\": \"91965632525\"\n" +
" },\n" +
" \"6\": {\n" +
" \"mobileContact\": \"965436222\",\n" +
" \"systemContact\": \"91965436222\"\n" +
" }\n" +
"}";
Map<String, Item> itemMap = new HashMap<>();
itemMap = new Gson().fromJson(data, itemMap.getClass());
Log.i("data", itemMap);
Item Class
private class Item {
String mobileContact;
String systemContact;
// getters and setters
public String getMobileContact() {
return mobileContact;
}
public void setMobileContact(String mobileContact) {
this.mobileContact = mobileContact;
}
public String getSystemContact() {
return systemContact;
}
public void setSystemContact(String systemContact) {
this.systemContact = systemContact;
}
}
You need to add the following to the build.gradle file,
compile 'com.google.code.gson:gson:2.8.0'

how should i make JSONObject and JSONArray from json url or json

My JSON:
{
"response_code": 200,
"error": false,
"train_name": "KCG YPR EXP",
"train_num": "17603",
"pnr": "1234567890",
"failure_rate": 19.346153846153847,
"doj": "20-8-2015",
"chart_prepared": "Y",
"class": "SL",
"total_passengers": 2,
"train_start_date": {
"month": 8,
"year": 2015,
"day": 20
},
"from_station": {
"code": "KCG",
"name": "KACHEGUDA"
},
"boarding_point": {
"code": "KCG",
"name": "KACHEGUDA"
},
"to_station": {
"code": "YPR",
"name": "YESVANTPUR JN"
},
"reservation_upto": {
"code": "YPR",
"name": "YESVANTPUR JN"
},
"passengers": [
{
"no": 1,
"booking_status": "S7,58,GN",
"current_status": "S7,58",
"coach_position": 9
},
{
"no": 2,
"booking_status": "S7,59,GN",
"current_status": "S7,59",
"coach_position": 9
}
]
}
What I did so far:
if(jsonStr!=null)
{
try
{
JSONObject object=new JSONObject(jsonStr);
JSONArray jsonArray=object.getJSONArray("passengers");
for (int i=0;i<=jsonArray.length();i++)
{
JSONObject o=jsonArray.getJSONObject(i);
int no= Integer.parseInt(o.optString("no").toString());
String booking_status=o.getString("Booking Status");
String current_status=o.getString("Current Status");
String coach_position=o.getString("Coach Position");
JSONObject data=o.getJSONObject("details");
String train_name = data.getString("Train Name");
int train_num = Integer.parseInt(data.optString("train number").toString());
int pnr = Integer.parseInt(data.optString("pnr").toString());
String chart_prepared = data.getString("Chart Prepared");
int total_passengers=Integer.parseInt(data.optString("total passengers").toString());
JSONObject date=data.getJSONObject("tarin_start_date");
int month=Integer.parseInt(data.optString("month").toString());
int year=Integer.parseInt(data.optString("year").toString());
int day=Integer.parseInt(data.optString("day").toString());
JSONObject co=date.getJSONObject("from_station");
String code=co.getString("code");
String name=co.getString("name");
JSONObject co1=co.getJSONObject("boarding_point");
String code1=co1.getString("code");
String name1=co1.getString("name");
JSONObject co2=co1.getJSONObject("to_station");
String code2=co2.getString("code");
String name2=co2.getString("name");
JSONObject co3=co2.getJSONObject("from_station");
String code3=co3.getString("code");
String name3=co3.getString("name");
HashMap<String,String> dtail=new HashMap<>();
dtail.put("no", String.valueOf(no));
dtail.put("Booking Staus",booking_status);
dtail.put("Current Staus",current_status);
dtail.put("Coach Positon",coach_position);
dtail.put("Train Name",train_name);
dtail.put("Train Number", String.valueOf(train_num));
dtail.put("pnr", String.valueOf(pnr));
dtail.put("chart prepared",chart_prepared);
dtail.put("total passengers", String.valueOf(total_passengers));
dtail.put("month", String.valueOf(month));
dtail.put("year", String.valueOf(year));
dtail.put("day", String.valueOf(day));
dtail.put("code",code);
dtail.put("name",name);
dtail.put("code",code1);
dtail.put("name",name1);
dtail.put("code",code2);
dtail.put("name",name2);
dtail.put("code",code3);
dtail.put("name",name3);
dataList.add(dtail);
}
}
catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
}
else
{
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG).show();
}
});
}
return null;
}
Here is your solution.
Hope this will help you. Whenever you work with JSON check all key name. It should always same as response key name
if (jsonStr != null) {
try {
JSONObject object = new JSONObject(jsonStr);
HashMap<String, String> dtail = new HashMap<>();
JSONArray jsonArray = object.getJSONArray("passengers");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject o = jsonArray.getJSONObject(i);
int no = Integer.parseInt(o.optString("no").toString());
String booking_status = o.getString("booking_status");
String current_status = o.getString("current_status");
String coach_position = o.getString("coach_position");
dtail.put("no", String.valueOf(no));
dtail.put("Booking Staus", booking_status);
dtail.put("Current Staus", current_status);
dtail.put("Coach Positon", coach_position);
}
String train_name = object.getString("train_name");
int train_num = Integer.parseInt(object.optString("train_num").toString());
int pnr = Integer.parseInt(object.optString("pnr").toString());
String chart_prepared = object.getString("chart_prepared");
int total_passengers = Integer.parseInt(object.optString("total_passengers").toString());
JSONObject date = object.getJSONObject("train_start_date");
int month = Integer.parseInt(date.optString("month").toString());
int year = Integer.parseInt(date.optString("year").toString());
int day = Integer.parseInt(date.optString("day").toString());
JSONObject co = object.getJSONObject("from_station");
String code = co.getString("code");
String name = co.getString("name");
JSONObject co1 = object.getJSONObject("boarding_point");
String code1 = co1.getString("code");
String name1 = co1.getString("name");
JSONObject co2 = object.getJSONObject("to_station");
String code2 = co2.getString("code");
String name2 = co2.getString("name");
JSONObject co3 = object.getJSONObject("reservation_upto");
String code3 = co3.getString("code");
String name3 = co3.getString("name");
dtail.put("Train Name", train_name);
dtail.put("Train Number", String.valueOf(train_num));
dtail.put("pnr", String.valueOf(pnr));
dtail.put("chart prepared", chart_prepared);
dtail.put("total passengers", String.valueOf(total_passengers));
dtail.put("month", String.valueOf(month));
dtail.put("year", String.valueOf(year));
dtail.put("day", String.valueOf(day));
dtail.put("code", code);
dtail.put("name", name);
dtail.put("code", code1);
dtail.put("name", name1);
dtail.put("code", code2);
dtail.put("name", name2);
dtail.put("code", code3);
dtail.put("name", name3);
dataList.add(dtail);
} catch (final JSONException e) {
Log.e("Json", "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
Hope this will help :-
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dummy);
String str = "{\n" +
" \"response_code\": 200,\n" +
" \"error\": false,\n" +
" \"train_name\": \"KCG YPR EXP\",\n" +
" \"train_num\": \"17603\",\n" +
" \"pnr\": \"1234567890\",\n" +
" \"failure_rate\": 19.346153846153847,\n" +
" \"doj\": \"20-8-2015\",\n" +
" \"chart_prepared\": \"Y\",\n" +
" \"class\": \"SL\",\n" +
" \"total_passengers\": 2,\n" +
" \"train_start_date\": {\n" +
" \"month\": 8,\n" +
" \"year\": 2015,\n" +
" \"day\": 20\n" +
" },\n" +
" \"from_station\": {\n" +
" \"code\": \"KCG\",\n" +
" \"name\": \"KACHEGUDA\"\n" +
" },\n" +
" \"boarding_point\": {\n" +
" \"code\": \"KCG\",\n" +
" \"name\": \"KACHEGUDA\"\n" +
" },\n" +
" \"to_station\": {\n" +
" \"code\": \"YPR\",\n" +
" \"name\": \"YESVANTPUR JN\"\n" +
" },\n" +
" \"reservation_upto\": {\n" +
" \"code\": \"YPR\",\n" +
" \"name\": \"YESVANTPUR JN\"\n" +
" },\n" +
" \"passengers\": [\n" +
" {\n" +
" \"no\": 1,\n" +
" \"booking_status\": \"S7,58,GN\",\n" +
" \"current_status\": \"S7,58\",\n" +
" \"coach_position\": 9\n" +
" },\n" +
" {\n" +
" \"no\": 2,\n" +
" \"booking_status\": \"S7,59,GN\",\n" +
" \"current_status\": \"S7,59\",\n" +
" \"coach_position\": 9\n" +
" }\n" +
" ]\n" +
"}";
parseJson(str);
}
private void parseJson(String jsonStr) {
HashMap<String, String> dtail = new HashMap<>();
if (jsonStr != null) {
try {
JSONObject object = new JSONObject(jsonStr);
int responseCode = object.getInt("response_code");
boolean error = object.getBoolean("error");
String train_name = object.getString("train_name");
int train_num = object.getInt("train_num");
int pnr = object.getInt("pnr");
double failurerate = object.getDouble("failure_rate");
String dateofJoin = object.getString("doj");
String chart = object.getString("chart_prepared");
String Class = object.getString("class");
int total_passengers = object.getInt("total_passengers");
JSONObject date = object.getJSONObject("train_start_date");
int month = date.getInt("month");
int year = date.getInt("year");
int day = date.getInt("day");
JSONObject co = object.getJSONObject("from_station");
String code = co.getString("code");
String name = co.getString("name");
JSONObject co1 = object.getJSONObject("boarding_point");
String code1 = co1.getString("code");
String name1 = co1.getString("name");
JSONObject co2 = object.getJSONObject("to_station");
String code2 = co2.getString("code");
String name2 = co2.getString("name");
JSONObject co3 = object.getJSONObject("from_station");
String code3 = co3.getString("code");
String name3 = co3.getString("name");
JSONArray jsonArray = object.getJSONArray("passengers");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
int no = jsonObject.getInt("no");
String booking_status = jsonObject.getString("booking_status");
String current_status = jsonObject.getString("current_status");
int coach_position = jsonObject.getInt("coach_position");
dtail.put("no", String.valueOf(no));
dtail.put("booking_status", booking_status);
dtail.put("current_status", current_status);
dtail.put("coach_position", String.valueOf(coach_position));
}
dtail.put("responseCode", String.valueOf(responseCode));
dtail.put("error", "" + error);
dtail.put("Train Name", train_name);
dtail.put("dateofJoin", dateofJoin);
dtail.put("chart", chart);
dtail.put("Class", Class);
dtail.put("Train Number", String.valueOf(train_num));
dtail.put("pnr", String.valueOf(pnr));
dtail.put("FAILURE_RATE", String.valueOf(failurerate));
dtail.put("total passengers", String.valueOf(total_passengers));
dtail.put("month", String.valueOf(month));
dtail.put("year", String.valueOf(year));
dtail.put("day", String.valueOf(day));
dtail.put("code", code);
dtail.put("name", name);
dtail.put("code", code1);
dtail.put("name", name1);
dtail.put("code", code2);
dtail.put("name", name2);
dtail.put("code", code3);
dtail.put("name", name3);
Log.e("Result------", "" + dtail);
} catch (final JSONException e) {
Log.e("tag", "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
}
}

How to display data?

I am using this method to call my service in my application.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
String url = "url";
AQuery mAQuery = new AQuery(Next.this);
mAQuery.ajax(url, String.class, new AjaxCallback<String>() {
#Override
public void callback(String url, String data, AjaxStatus status) {
super.callback(url, data, status);
if (BuildConfig.DEBUG) {
Log.d("###$Request URL", url + "");
Log.d("###$Response ", data + "");
Log.d("###$Status Message : ", status.getMessage() + "");
Log.d("###$Status Code : ", status.getCode() + "");
}
if (null != data && status.getCode() != -101) {
String StringData = "" + data;
try {
JSONObject json = new JSONObject(StringData);
String COMP_REQ_ID = json.getString("COMP_REQ_ID");
String CompanyName = json.getString("CompanyName");
String COMP_REQ_TYPE = json.getString("COMP_REQ_TYPE");
String Name = json.getString("Name ");
myAwesomeTextview.setText("COMP_REQ_ID: " + COMP_REQ_ID + "\n" + "CompanyName:" + CompanyName + "\n" + "COMP_REQ_TYPE: " + COMP_REQ_TYPE + "\n" + "Name : " + Name);
} catch (JSONException e) {
myAwesomeTextview.setText("" + e);
}
But the data coming from server is not getting display on my phone screen.
The data i got from my service is given below:
[{"Name":null,"PositionName":null,"DateOfEvent":null,"EvetnId":0,"HetId":0,"EventDate":null,"COMP_REQ_ID":9714,"COMP_REQ_TYPE":"Intership","JobTitle":"Administrator","CompanyName":"Jensor's International (Ltd).","ReqQualification":"","DegreeName":"B.E/B.Tech,M.C.A,M.B.A,B.A,B.A.M.S,B.Com,B.S.W","Post_Status":1,"Eventdate":"21/06/2016","JobsOrInternships":null},{"Name":null,"PositionName":null,"DateOfEvent":null,"EvetnId":0,"HetId":0,"EventDate":null,"COMP_REQ_ID":9713,"COMP_REQ_TYPE":"Intership","JobTitle":"junior counselor","CompanyName":"Jensor's International (Ltd).","ReqQualification":"","DegreeName":"B.E/B.Tech,M.C.A,M.B.A,B.B.M,B.Com,B.F.A","Post_Status":1,"Eventdate":"21/06/2016","JobsOrInternships":null}
How to display it.
You should use JSONArray to parse your result
JSONArray rootArray = new JSONArray(jsonString);
int len = rootArray.length();
for(int i = 0; i < len; ++i) {
JSONObject json = rootArray.getJSONObject(i);
String COMP_REQ_ID = json.getString("COMP_REQ_ID");
}
Or you can use GSon library to parse the result for you. I recommend this post as example how to query data from ASP.NET Web API.
I think you should make your question clear because I'm not sure if you have problem with getting data or showing data to ui.
public class ArrayBean {
public String Name;
public String PositionName;
public String DateOfEvent;
public String EvetnId;
public String HetId;
public String EventDate;
public String COMP_REQ_ID;
public String COMP_REQ_TYPE;
public String JobTitle;
public String CompanyName;
public String ReqQualification;
public String DegreeName;
public String Eventdate;
public String JobsOrInternships;
}
ArrayBean bean;
JSONArray array=new JSONArray("StringData ");
JSONObject json;
for(int i=0;i<array.length();i++){
bean=new ArrayBean();
json=new JSONObject();
json=array.getJSONObject(i);
bean.COMP_REQ_ID=json.getString("COMP_REQ_ID");
bean.COMP_REQ_TYPE=json.getString("COMP_REQ_TYPE");
bean.CompanyName=json.getString("CompanyName");
bean.DateOfEvent=json.getString("DateOfEvent");
bean.EventDate=json.getString("EventDate");
bean.EvetnId=json.getString("EvetnId");
bean.HetId=json.getString("HetId");
bean.JobsOrInternships=json.getString("JobsOrInternships");
bean.JobTitle=json.getString("JobTitle");
bean.Name=json.getString("Name");
bean.PositionName=json.getString("PositionName");
bean.ReqQualification=json.getString("ReqQualification");
}
Note, care about data-types in JSON e.g key COMP_REQ_ID has value data-type is int. You should use optString or optInt instead of getString or getInt to parse your result
JSONArray rootArray = new JSONArray(jsonString);
int len = rootArray.length();
for(int i = 0; i < len; ++i) {
JSONObject json = rootArray.optJSONObject(i);
int COMP_REQ_ID = json.optInt("COMP_REQ_ID");
String COMP_REQ_TYPE = json.optString("COMP_REQ_TYPE");
...
}
i used this code
JSONArray rootArray = new JSONArray(StringData);
int len = rootArray.length();
for (int i = 0; i < len; ++i) {
JSONObject json = rootArray.optJSONObject(i);
int COMP_REQ_ID = json.optInt("COMP_REQ_ID");
String COMP_REQ_TYPE = json.optString("COMP_REQ_TYPE");
String CompanyName = json.getString("CompanyName ");
String Name = json.getString("Name");
String PositionName = json.getString("PositionName");
myAwesomeTextview.setText("COMP_REQ_ID:" + COMP_REQ_ID + "\n" + "COMP_REQ_TYPE" + COMP_REQ_TYPE + "\n" + "CompanyName" + CompanyName + "\n" + "Name" + Name + "\n" + "PostionNmae" + PositionName);
}
But then also the result is not diplayed on my phone And in android studio logcat it showing me this:
reporting:java.lang.NullPointerException at com.example.anand.Next$1.callback(Next.java:55)
at com.example.anand.Next$1.callback(Next.java:24)
at

how to get value from nameless array of Json

String Result=GET(url);
Log.e("result", "data" + Result);
try{
//JSONObject jsonResponse = new JSONObject(Result);
// JSONArray jsonMainNode = jsonResponse.optJSONArray();
//int len= jsonMainNode.length();
JSONArray jsonarray = new JSONArray(Result);
for (int i = 0; i < 3; i++) {
JSONObject jsonobj = jsonarray.getJSONObject(i);
System.out.println("categoryId : " + i + " = " + jsonobj.getString("FirstName"));
System.out.println("Title : " + i + " = " + jsonobj.getString("LastName"));
//System.out.println("songs : " + i + " = " + jsonobj.getString("songs"));
}
Note: I am trying this to get object of nameless array. I am not getting how to store the response as a form of Json object
Try this it works for me.
try {
JSONArray jarr = new JSONArray(Result);
for (int i = 0; i < jarr.length(); i++) {
JSONObject jsonobj= jarr.getJSONObject(i);
System.out.println("categoryId : " + i + " = " + jsonobj.getString("FirstName"));
System.out.println("Title : " + i + " = " + jsonobj.getString("LastName"));
}
}

Categories

Resources