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

Using seekbars to change RGB values android studio
How do you use a URL string to update an ImageView?
Adding OnClick listener to the view in android
How to display icon of songs in media player similar to you tube in android studio [closed]
Android startactivityforresult not called after permissionrequest
Video not play on mobile
Has Firebase a library for download videos that are on Firebase Storage?
intent share facebook - Android
how to use onPageCommitVisible under api 23 in Android
version Name Suffix
Set animation when change position viewpager with animation
How to set the notification just send once every year?
Android launch application with custom credentials
Android : library that download many sort of files
Leakcanary -can removing weak references helps to avoid memory leaks
How to stop an observable upon click in RxAndroid?

Categories

HOME
kentico
scikit-learn
checkstyle
ecmascript-6
gpu
redux-form
twig
static
gradient
sublimetext
square
google-admin-sdk
angularfire2
frontend
bing-maps
pattern-matching
stellar.js
endpoint
favicon
code-generation
dynatrace
event-sourcing
dreamweaver
wolframalpha
foreign-keys
symfony-console
symbols
breadth-first-search
gmock
motion-detection
fractions
intersystems-cache
winexe
tpm
bioinformatics
uitapgesturerecognizer
spring-security4
mailkit
login-script
joomla2.5
slacktextviewcontroller
nohup
amazon-ses
odroid
metaprogramming
cin
visa
levenshtein-distance
movilizer
photos
apache-spark-dataset
procfile
plan-9
user-defined-fields
bluez
podscms
remedy
scalding
qtranslate
context-sensitive-grammar
java-websocket
baidu
juice-ui
cvxpy
pure-css
segment-io
eigenvector
swizzling
nss
createprocess
ellucian-scribe
assertion
jdi
django-urls
canalyzer
jsvc
cryptographichashfunction
jbake
administration
webfinger
template-deduction
winginx
terminate
authlogic
rfc5322
gwt-openlayers
light
facebook-timeline
idn
android-loadermanager
midlet
zope.interface
failing-tests
isa-swizzling
procedures
jquery-tools
saxparseexception
filemerge
collectionviewsource
ie-compatibility-mode
non-clustered-index
opml
wysiwym

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