android


How to parse json with unknown key by Retrofit?


I want to parse this JSON by Retrofit (i don't know field names{xxx,yyy,zzz})
I tried some Maps, but I did something wrong.
Please help me parse this damn json
{
xxx: [
{
name: "name1",
description: "desc1"
}
],
yyy: [
{
name: "name2",
description: "desc2"
}
],
zzz: [
{
name: "name3",
description: "desc3"
},
{
name: "name4",
description: "desc4"
}
]
}
---Solution---
I tried create class responce, but it wrong way
public class DishesCategoryResponse {
public Map<String, List<Dish>> settingsMap;
}
Then i tried this and it works
#GET("/api/restaurant_menu/{id}")
Observable<Map<String, List<Dish>>> getDishesCategory(#Path("id") long id);
One day I had a similar task. Ain't sure my solution is perfect, but it may help you.
I had this json file:
final JSONObject bodyObject = new JSONObject(body);
final JSONObject activities = bodyObject.getJSONObject("activities");
final Iterator<String> keys = activities.keys(); // you can iterate through all keys
final List<ContactActivity> contactActivityInfoList = new ArrayList<ContactActivity>();
while (keys.hasNext()) {
final String key = keys.next();
final String jsonString = activities.getJSONObject(key).toString();
final ContactActivity contactActivity =
mGson.fromJson(jsonString, ContactActivity.class);
contactActivityInfoList.add(contactActivity);
}
Using a tool like GsonFormat (Android studio plugin), this is the class that maps your json:
public class YourClassName {
/**
* name : name1
* description : desc1
*/
private List<XxxEntity> xxx;
/**
* name : name2
* description : desc2
*/
private List<YyyEntity> yyy;
/**
* name : name3
* description : desc3
*/
private List<ZzzEntity> zzz;
public void setXxx(List<XxxEntity> xxx) {
this.xxx = xxx;
}
public void setYyy(List<YyyEntity> yyy) {
this.yyy = yyy;
}
public void setZzz(List<ZzzEntity> zzz) {
this.zzz = zzz;
}
public List<XxxEntity> getXxx() {
return xxx;
}
public List<YyyEntity> getYyy() {
return yyy;
}
public List<ZzzEntity> getZzz() {
return zzz;
}
public static class XxxEntity {
private String name;
private String description;
public void setName(String name) {
this.name = name;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
}
public static class YyyEntity {
private String name;
private String description;
public void setName(String name) {
this.name = name;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
}
public static class ZzzEntity {
private String name;
private String description;
public void setName(String name) {
this.name = name;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
}
}

Related Links

Android, layout screen width
AsyncTask inside a Static method - Good Coding Practice?
Is there a programmatic way to update an android app on the android market?
Calling a native method from Android project
How do you save the data of an int, and then edit the number onclick of a button?
connect activitys
mixing SQLite DB and GetSharedPreferences()
Android Couchbase - URI for attachments
Trying to use Toast message, but get error
How to deploy android plugin that can be loaded from my application
Display Android Preferences in Different Units
SOCKet Secure (SOCKS) server for Android
Android: NullPointer Exception on recreating the alarm on OS Bootup?
Android with thread shows error
Android Circular Motion of a Device
Android application for Tablet

Categories

HOME
uml
beautifulsoup
phpword
prism
wakanda
github-for-windows
pyqt
themes
package
alsa
vxml
interpolation
python-multithreading
crystal-lang
32bit-64bit
openstreetmap
octave
github-enterprise
gruntfile
azure-powershell
mathprog
imessage
sse
sencha-touch
boost-icl
drive
user-experience
poedit
scriptella
interbase
oculus
datastax-enterprise-graph
testcomplete
calculation
jbutton
code-snippets
linkedhashmap
dsx-desktop
android-maps-extensions
reverse
jwplayer6
navigator
apiary.io
docfx
wurfl
protein-database
hackintosh
placeholder
automapper-5
launchctl
twilio-click-to-call
stdmap
cup
sandcastle
polyline
taocp
segment-io
lrs
installshield-2010
expectations
http-status-code-401
nservicebus5
spreadjs
halcon
jaunt-api
lazybones
baasbox
linked-tables
apache-commons-daemon
keyup
omnifaces
google-refine
git-ftp
teaspoon
tweenlite
jama
jquery-slider
fpdi
dsym
spring-3
eclipse-kepler
light
breakout
ice-cube
ui-select2
nebula
merb
pclzip
bjyauthorize
uimanageddocument
os.system
jdb
isa-swizzling
box2d-iphone
google-maps-mobile
screen-size
catransform3d
django-paypal
office-2007
product-management
xslcompiledtransform
idatareader

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App