android


How to Parse the JSON response which contains the two arraylist


I am having the response of one of webservice to get the feeds list and in that response i am getting the two array with the same name.
The problem is that i am not able get the details of the inner array i.e. "ProfileName", "ImageUrl" etc..
I have tried the json parsing.
Below is the response of JSON:
{"Status":true,
"result":
[
{"result": [
{"ProfileName":"followers5","ImageUrl":"http:\/\/192.168.0.1\/webservice2\/uploads\/81.png","Likes":3,"Hearts":2},
{"ProfileName":"followers5","VideoUrl":"http:\/\/192.168.0.1\/webservice2\/uploads\/81.mp4","Likes":0,"Hearts":0}
]}
,{"result":[
{"ProfileName":"followers6","ImageUrl":"http:\/\/192.168.0.1\/webservice2\/uploads\/82.png","Likes":0,"Hearts":2},
{"ProfileName":"followers6","VideoUrl":"http:\/\/192.168.0.1\/webservice2\/uploads\/82.mp4","Likes":0,"Hearts":0}
]}
]
}
I have tried as below:
class feedtask extends AsyncTask<String, Integer, String> {
#Override
protected String doInBackground(String... params) {
Httputils getmethod = new Httputils();
try {
result = getmethod.Getrequest("feeds.php?uid=76");
System.out.println("Feeds Result--->" + result);
JSONObject jobj = new JSONObject(result);
JSONArray ja = jobj.getJSONArray("result");
for (int i = 0; i < ja.length(); i++) {
for (int j = 0; j <= i; j++) {
JSONObject jo = ja.getJSONObject(j);
abcd.add(jo.getString("ProfileName"));
System.out.println("Profile Name--->"
+ jo.getString("ProfileName"));
}
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
Please help me out to get the details of the second array.
Any help will be appreciated.
Thank you.
try{
JSONObject responseJson = new JSONObject(response);
if(responseJson.has("result")){
JSONArray resultJsonArr = responseJson.getJSONArray("result");
for(int i=0; i<resultJsonArr.length(); i++){
JSONObject resultInstanceJson = resultJsonArr.getJSONObject(i);
if(resultInstanceJson.has("result")){
JSONArray resultArr = resultInstanceJson.getJSONArray("result");
for(int j=0; j<resultArr.length(); j++){
JSONObject jsonResult = resultArr.getJSONObject(j);
String profileNAme = jsonResult.getString("ProfileName");
String imageUrl = "", videoUrl = "";
//image url
if(jsonResult.has("ImageUrl")){
imageUrl = jsonResult.getString("ImageUrl");
}
//video url
if(jsonResult.has("VideoUrl")){
videoUrl = jsonResult.getString("VideoUrl");
}
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}
try
{
JSONObject _JSONResponse=new JSONObject(response);
String status=_JSONResponse.getString("Status");
JSONArray _ArrayResponse=_JSONResponse.getJSONArray("result");
for(int i=0;i<_ArrayResponse.length();i++)
{
JSONArray object=_ArrayResponse.getJSONObject(i).getJSONArray("result");
for(int j=0;j<object.length();j++)
{
JSONObject jObject=object.getJSONObject(j);
ProfileName=jObject.getString("ProfileName") ;
VideoUrl=jObject.optString("VideoUrl") ;
Likes=jObject.optString("Likes") ;
Hearts=jObject.optString("Hearts") ;
Log.i(TAG,ProfileName +" "+VideoUrl +" "+Likes+" " +Hearts);
}
}
}
catch(JSONException e){Log.e(TAG,e.toString());}
try this way
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = ja.getJSONObject(i);
JSONArray innerResult = jo.getJSONArray("result");
int size = innerResult.legth();
for (int j = 0; j < size; j++) {
JSONObject innerJo = innerResult.getJSONObject(j);
abcd.add(innerJo.getString("ProfileName"));
}
}
Edit:
public class InfoHolder {
public String profileName;
public String imageUrl;
public String videUrl;
}
for (int j = 0; j < size; j++) {
InfoHolder holder = new InfoHolder();
JSONObject innerJo = innerResult.getJSONObject(j);
// the same for imageUrl and videoUrl
holder.profileName = innerJo.getString("ProfileName");
abcd.add(holder);
}
of course you have to change from String to InfoHolder your abcd collectio
Try this:
try {
result = getmethod.Getrequest("feeds.php?uid=76");
System.out.println("Feeds Result--->" + result);
JSONObject jobj = new JSONObject(result);
JSONArray ja = jobj.getJSONArray("result");
for (int i = 0; i < ja.length(); i++) {
for (int j = 0; j < i; j++) {
JSONObject jo = ja.getJSONObject(j);
JSONArray resultJA = jo.getJSONArray("result");
for(int k=0;k< resultJA.length();k++){
JSONObject jo_inside = resultJA.getJSONObject(k);
String profileName = jo_inside.getString("ProfileName");
//use optString on JSONOBject. it will return empty String if that key does not exists or else the value u want. it won't give any exceptions.
String ImageURL = jo_inside.optString("ImageUrl");
String VideoURL = jo_inside.optString("VideoUrl");
}
}
}
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
JSONObject obj1 = new JSONObject(url);
try {
JSONArray result = obj1.getJSONArray("result");
for(int i=0;i<=result.length;i++)
{
JSONObject obj2 = result.getJSONObject(i);
JSONArray result1 = obj2.getJSONArray("result");
String ProfileName=result1.getString("ProfileName");
String ImageUrl=result1.getString("ImageUrl");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You do
for (int i = 0; i < ja.length(); i++) {
ok for the first array now you need to get the array inside each cells of the first
For example :
JSONObject jo = ja.getJSONObject(i);
JSONArray ja2 = jo.getJSONArray("result");
then and only then you do the second loop
Maybe this answer isn't complete but the idea is here

Related Links

daimajia AndroidSwipeLayout issue
Bottom NavigationView change items programmatically
Android Studio : How can I export lint-results.xml as a single file
[Android]Add AdView to my UI layout
Android Twilio chat getChannel 403 error
Android custom ringtone not setting programaticaly on some devices
ViewPager (with pagetransformer animation) crashes while rotating screen
How to override default call screen?
java.lang.OutOfMemoryError:pthread_create(1040kb stack) failed: Out of memory
How can I get exact colors of PNG in android?
Firebase MultiSelect Delete from ListView
sqlite database delete not working
Receiving data via Bluetooth and viewing it on app screen
why Error:(2) No resource identifier found for attribute 'shortcutShortLabel' in package 'android' when creating shourtcut in Android7?
How to set the Android RecyclerView to display the items in the center of the screen
what is the name of this part of android screen?

Categories

HOME
spark-streaming
cheerio
polymer
time-complexity
apache-jena
amp-html
character-encoding
interface
ms-access-2016
entity-framework-core
simulink
gembox-spreadsheet
webix
flume
modal-dialog
special-characters
okhttp
ycsb
cmdb
radgridview
code-generation
dynatrace
eip
esoteric-languages
package.json
vuejs
connector
swagger-2.0
enterprise-miner
user-experience
r-mice
angular-routing
xunit
data.stackexchange.com
syntax-highlighting
angularjs-components
asymptotic-complexity
nstimer
handler
scrollview
reactjs.net
protege4
git-rewrite-history
dm-script
xdocreport
apriori
movilizer
apollostack
signal-strength
ovf
activeperl
mongotemplate
bluez
boo
pdf-conversion
donations
choice
taocp
pure-css
cubism.js
redbeard-ios
mongodb-php
riot
fuzzywuzzy
database-relations
halcon
nio2
aspen
jparsec
design-principles
dynatree
xenomai
baasbox
airbrake
delayed-execution
filesplitting
big-ip
paw
universal-image-loader
wxformbuilder
dot42
decoupling
git-repo
getopt
light
gd-graph
tlf
pagedown
wp7test
stacky
swfobject
wimax
virtualpathprovider
winmain
tmx
facebook-iframe
drupal-fivestar
aspmenu
eclipse-marketplace
qtabbar
pagemethods
int64
pbcopy
mathematical-typesetting

Resources

Encrypt Message