How can i parse this jsonarray into recyclerview? [duplicate] - android

This question already has answers here:
Parse JSON Array without Key in Android
(3 answers)
Closed 4 years ago.
Please Help me How can I parse this type of json response in recyclerview in android? Please give me answer i search many but i not get any answer
{
"data1": [
"",
"",
"",
""
],
"data2": [
"aaa",
"aaa",
"aaa",
"aaa"
],
"data3": [
"bbb",
"bbb",
"bbb",
"bbb"
]
}
i try this but only add last position data not all data
try {
Log.e("res_gallery123456", responseString);
JSONObject jsonObj = new JSONObject(responseString);
BuyData buydata = new BuyData();
JSONArray data1= jsonObj.getJSONArray("data1");
for (int i = 0;i<data1.length();i++){
buydata.setTxt_img(data1.getString(i));
JSONArray data2=jsonObj.getJSONArray("data2");
for (int j = 0;j<data2.length();j++){
buydata.setTxt_price(data2.getString(j));
JSONArray data3= jsonObj.getJSONArray( "data3");
for (int k = 0;k<data3.length();k++) {
buydata.setTxt_ammount(data3.getString(k));
}
}
buyDataList.add(buydata);
dialog.dismiss();
}
} catch (JSONException e) {
dialog.dismiss();
e.printStackTrace();
}

Update the code
try {
Log.e("res_gallery123456", responseString);
JSONObject jsonObj = new JSONObject(responseString);
JSONArray data1= jsonObj.getJSONArray("data1");
JSONArray data2= jsonObj.getJSONArray("data2");
JSONArray data3= jsonObj.getJSONArray("data3");
for (int i = 0;i<data1.length();i++){
BuyData buydata = new BuyData();
buydata.setTxt_img(data1.getString(i));
buydata.setTxt_price(data2.getString(i));
buydata.setTxt_ammount(data3.getString(i));
buyDataList.add(buydata);
dialog.dismiss();
}
} catch (JSONException e) {
dialog.dismiss();
e.printStackTrace();
}

Related

How to get json object and array values?

How to parse JSON values in this format? I want to get the details of the data element but inside data there are 'dates' and inside dates there is array containing two more elements. I want to get all the dates first inside data and then within these dates I want all the information within these dates. How can I achieve this? Please Help. I tried with below code but it hasn't worked
try {
JSONObject jsonObject = new JSONObject("data");
JSONArray jsonArray =jsonObject.getJSONArray(String.valueof(cuurentdate));
JSONArray session;
for (int i = 0; i < jsonArray.length() - 1; i++) {
jsonObject = jsonArray.getJSONObject(i);
session= jsonObject.getJSONArray("session");
Log.d("MyLog", session + "");
}
} catch (JSONException e) {
e.printStackTrace();
}
Following is the format
{
"status": 1,
"status_code": 200,
"data": {
"2018-02-11": [
{
"session": "01:00 AM",
"place": true
},
{
"session": "02:00 AM",
"place": true
}
],
"2018-02-12": [
{
"session": "01:00 AM",
"place": true
},
{
"session": "02:00 AM",
"place": true
}
]
}
}
You just need to pass the response string to the method. You can try this:
private void jsonParsing(String jsonString) {
// String jsonString = "{ \"status\": 1, \"status_code\": 200, \"data\": { \"2018-02-11\": [ { \"session\": \"01:00 AM\", \"place\": true }, { \"session\": \"02:00 AM\", \"place\": true } ], \"2018-02-12\": [ { \"session\": \"01:00 AM\", \"place\": true }, { \"session\": \"02:00 AM\", \"place\": true } ] } }";
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject dataObj = jsonObject.getJSONObject("data");
Iterator<String> iter = dataObj.keys();
Log.e(TAG, "jsonParsing: "+iter );
while (iter.hasNext()) {
String key = iter.next();
JSONArray datesArray = dataObj.getJSONArray(key);
ArrayList<String> sessions = new ArrayList<String>();
for (int i = 0; i < datesArray.length(); i++) {
JSONObject datesObject = datesArray.getJSONObject(i);
sessions.add(datesObject.getString("session"));
}
Log.d("MyLog", sessions + "");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
(1) get JSONObject of Main json
JSONObject objMain = new JSONObject("your json string");
(2)get JSONObject of "data" from main json
JSONObject jsonData = objMain.getJSONObject("data")
(3) get all keys (dates) from object "data"
Iterator<String> iter = jsonData.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
JSONArray arrayDate = objData.getJSONArray(key)
for (i = 0; i < arrayDate.length(); i++) {
JSONObject objDate = arrayDate.getJSONObject(i)
Log.d("#session :", "" + objDate.getString("session"))
Log.d("#place :", "" + objDate.getBoolean("place"))
}
} catch (JSONException e) {
// Something went wrong!
}
}
try this one code
IN THIS CODE jsonMstObject IS TEMP OBJECT YOU HAVE TO USE YOUR API RESPONSE JSONobject INSTEAD OF jsonMstObject
try {
JSONObject jsonMstObject = new JSONObject("{"status":1,"status_code":200,"data":{"2018-02-11":[{"session":"01:00 AM","place":true},{"session":"02:00 AM","place":true}],"2018-02-12":[{"session":"01:00 AM","place":true},{"session":"02:00 AM","place":true}]}}");
JSONObject jsonObject = jsonMstObject.getJSONObject("data");
JSONArray jsonArray =jsonObject.getJSONArray(String.valueof(cuurentdate));
ArrayList<String> arrSession = new ArrayList<String>();
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = jsonArray.getJSONObject(i);
arrSession.add(jsonObject.getString("session"));
}
Log.d("MyLog", arrSession + "");
} catch (JSONException e) {
e.printStackTrace();
}
in this code arrSession is your session string array
Ex. you passed cuurentdate = "2018-02-11" then you recived result like
[01:00 AM, 02:00 AM]
Note: this code is worked based on your cuurentdate param. This is code for get static date array from data and create String Array.

How should I parse this kind of JSON?

I'm new to JSON parsing. It would be a great help if anyone would help me with parsing this kind of json array in Android.
Thank you
{
"response": 200,
"department": [
"Information Technology"
],
"subject": [
"ads(th)"
],
"professional": [
"cg(th)",
"cg(lab)"
],
"semester": [
"3A",
"5A",
"5A"
]
}
This is ur response:
{
"response": 200,
"department": [
"Information Technology"
],
"subject": [
"ads(th)"
],
"professional": [
"cg(th)",
"cg(lab)"
],
"semester": [
"3A",
"5A",
"5A"
]
}
U can do like this
String responseString="" //this string is ur web service response
try {
//JSON is the JSON code above
JSONObject jsonResponse = new JSONObject(responseString);
JSONArray department = jsonResponse.getJSONArray("department");
String hey = department.toString();
JSONArray subject = jsonResponse.getJSONArray("subject");
String sub = subject.toString();
JSONArray professional= jsonResponse.getJSONArray("professional");
String pro = professional.toString();
//like this u can parse other JsonArray
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
After getting those values in jsonarray u want to display it in spinner than u can do like this
ArrayList<String> listdata = new ArrayList<String>();
if (professional != null) {
for (int i=0;i<professional.length();i++){
listdata.add(professional.getString(i));
}
}
For Display into spinner
Spinner spinner = (Spinner) findViewById(R.id.SpinnerSpcial);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listdata);//Pass list data of Profession
spinner.setAdapter(adapter);
Notes:
You can make custom adapter also by extending BaseAdapter or ArrayAdapter for Spinner.
Hope this will help u ... if u have any questions u can ask
Try this,
try {
JSONObject obj_result=new JSONObject(result);
String response=obj_result.getString("response");
JSONArray arr_department=obj_result.getJSONArray("department");
for(int i=0;i<arr_department.length();i++)
{
String department_name=arr_department.getString(i);
Log.d("TAG","department_name:"+department_name);
}
JSONArray arr_subject=obj_result.getJSONArray("subject");
for(int i=0;i<arr_subject.length();i++)
{
String subject_name=arr_subject.getString(i);
Log.d("TAG","subject_name:"+subject_name);
}
JSONArray arr_professional=obj_result.getJSONArray("professional");
for(int i=0;i<arr_professional.length();i++)
{
String professional_name=arr_professional.getString(i);
Log.d("TAG","professional_name:"+professional_name);
}
JSONArray arr_semester=obj_result.getJSONArray("semester");
for(int i=0;i<arr_semester.length();i++)
{
String semester_name=arr_semester.getString(i);
Log.d("TAG","semester_name:"+semester_name);
}
} catch (JSONException e) {
e.printStackTrace();
}
try { //When parsing JSON, you need try catch to handle error if occure
JSONObject fullJSON = new JSONObject("{\"response\":200,\"department\":[\"Information Technology\"],\"subject\":[\"ads(th)\"],\"professional\":[\"cg(th)\",\"cg(lab)\"],\"semester\":[\"3A\",\"5A\",\"5A\"]}");
int response = fullJSON.getInteger("response");
JSONArray departement = fullJSON.getJSONArray("department");
Log.i("", departement.getString(0));
JSONArray semester = fullJSON.getJSONArray("semester");
for(int i=0; i<semester.length(); i++) {
Log.i("semester", semester.getString(i));
}
} catch (JSONException e) {
//here's the error message you can get from
e.printStackTrace();
}
I suggest you use Gson Libray
With Gson your mapping class will be like this:
public class YourClass {
#SerializedName("response")
private Integer response;
#SerializedName("department")
private List<String> department;
#SerializedName("subject")
private List<String> subject;
#SerializedName("professional")
private List<String> professional;
#SerializedName("semester")
private List<String> semester;
}
With Gson you can transform your Json in POJO (or vice versa) easily. Check the documentation.

Android parse json force stopped

I am parsing a json url: http://www.json-generator.com/api/json/get/bQmwsOmYeq?indent=2 but I failed to make it happen. I am getting an error. What's wrong with this code?
public void parseJsonResponse(String result) {
Log.i(TAG, result);
pageCount++;
try {
JSONObject json = new JSONObject(result);
JSONArray jArray = json.getJSONArray("name");
for (int i = 0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
CountryInfor country = new CountryInfor();
country.setId(jObject.getString("id"));
country.setName(jObject.getString("name"));
countries.add(country);
}
adapter.notifyDataSetChanged();
if (dialog != null) {
dialog.dismiss();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Replace
JSONObject json = new JSONObject(result);
JSONArray jArray = json.getJSONArray("name");
these two lines by
JSONArray jArray = new JSONArray(result);
Also change
country.setId(jObject.getString("id"));
by
country.setId(jObject.getInt("id") + "");
Because id is of type int not a String.
And it will work fine.
You are getting response like
[
{
"id": ​1,
"name": "Vietnam"
},
{
"id": ​2,
"name": "China"
},
{
"id": ​3,
"name": "USA"
},
{
"id": ​4,
"name": "England"
},
{
"id": ​10,
"name": "Russia"
}
]
So the response is JSONArray and not JSONObject.
Hope it'll help.
Edit
#Override
protected String doInBackground(Void... params) {
// call load JSON from url method
if(loadJSON(this.url) != null) {
return loadJSON(this.url).toString();
}
Log.d("LoadJSONFromUrl", "Failed to get JSONObject.");
return null;
}
You are getting JSONObject null, so the error is.
- You should know what is NullPointerException
the issue is with this line
JSONArray jArray = json.getJSONArray("name");
There is no array with name name. it is failing here. Correct this part to obtain the array properly. Rest looks fine.

how to get Category values from json in android? [duplicate]

This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 7 years ago.
{
"response":
{
"Category":
[
"Restaurant",
"Salon",
"Spa",
"Dry Cleaners",
"Car Spa",
"Hospitals"
]
}
}
You can do like this-
try {
String json = <Your Json String>;
JSONObject response = new JSONObject(json);
JSONArray category = response.getJSONArray("Category");
for (int i = 0; i < category.length(); i++) {
Log.d("categories are", category.getString(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
Try this , it can be Pseudo Code
String json = <YourJSon String>
JSONObject object = new JSONObject(json);
JSONArray array = object.getJSONArray("Category");
for(int i =0; i<array.size();i++){
String s = array.get(i).getString();
}

How to parse json with android

how to get json data with android
my json like this:
[
{
"meta": {
"next_id": 30
}
},
{
"data": [
{
"category_id": "2",
"name": "Anniversary Facebook Status",
"count": "53"
},
{
"category_id": "4",
"name": "April Fool Status",
"count": "16"
},
{
"category_id": "79",
"name": "Wise Facebook Status",
"count": "90"
}
]
}
]
My code
JSONObject jsonobject = JSONfunctions.getJSONfromURL("URL");
JSONArray jsonarray = jsonobject.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
Log.i("ID", jsonobject.getString("category_id"));
Log.i("Name", jsonobject.getString("name"));
}
i want get "data" array.
please help if anybody know:
Thank You.
Try this.
JSONArray total_array = new JSONArray(string json);
JSONArray data_array = total_array.getJSONObject(1).getJSONArray("data");
for(int i = 0; i < data_array.length(); i++){
JSONObject json_data = data_array.getJSONObject(i);
String category_id = json_data.getString("category_id");
}
Probably this may help you :
JSONArray jsonArray;
try {
jsonArray = new JSONArray("YOUR JSON DATA");
Log.v("MAIN ACTIVITY", "JSON OBJECT :"+jsonArray.getJSONObject(0).getJSONObject("meta").getString("next_id"));
JSONArray dArray = jsonArray.getJSONObject(1).getJSONArray("data");
for(int i=0;i<dArray.length();i++)
{
JSONObject jsonObject = dArray.getJSONObject(i);
Log.v("MAIN ACTIVITY", "JSON DATA :"+jsonObject.getString("category_id"));
Log.v("MAIN ACTIVITY", "JSON DATA :"+jsonObject.getString("name"));
Log.v("MAIN ACTIVITY", "JSON DATA :"+jsonObject.getString("count"));
}
}
catch (JSONException e1) {
e1.printStackTrace();
}
to get json array
JSONArray jsonArray = new JSONArray(jsonStr);
JSONArray dataArray= jsonArray.getJSONObject(1).getJSONArray("data");
for(int i=0;i<dataArray.length();i++){
JSONObject json_data = dataArray.getJSONObject(i);
String category_id = json_data.getString("category_id");
String name = json_data.getString("name");
String count = json_data.getString("count");
}
For more info see Android JSON Parsing Tutorial
Look into the Gson library by Google. It's easier than parsing the fields manually with the included JSON lab.

Categories

Resources