Reputation: 1872
I have gone through may posts about dynamic keys parsing. But still confusing on parsing below JSON format using Gson which is a new response in my existing project.
{
"position":0,
“source”:[“Win","Web","Capture","iOS","Camera","Back"],
“source_data”:{
"Web”:{“years”:[{“start”:1500,"end”:1700}]},
"Win":{"years":[{"start”:1700,"end”:2000}]},
"iOS":{"years":[{"start”:1700,"end”:2200}]},
"Back":{"years":[{"start”:2100,"end”:2300}]},
"Camera":{"years":[{"start”:2200,"end":2200},{"start”:2200,"end”:2600},{"start”:2300,"end”:2600}]},
"Capture":{"years":[{"start”:1700,"end”:2100}]}
}
}
I already having Json Parsing setup using Gson library. below is the code snippet with i am using to parse. but source_data key's value is not getting stored into my model class
@Expose
@SerializedName("source_data")
private SourceData mSourceData;
public class SourceData {
HashMap<String, SourceYears> mSourceYears;
public HashMap<String, SourceYears> getSourceYears() {
return mSourceYears;
}
public void setSourceYears(HashMap<String, SourceYears> years) {
this.mSourceYears = years;
}
}
public class SourceYears {
@Expose
@SerializedName("years")
private List<Years> mYears;
public List<Years> getYears() {
return mYears;
}
public void setYears(List<Years> years) {
this.mYears = years;
}
}
here Years
is the class with contain start
& end
keys.
please suggest if some one identified what i am doing wrong.
Upvotes: 0
Views: 83
Reputation: 6963
Please try to use this, i have refined models based on json you provided, also the json you provided is not accurate due to wrong double quotes.
Here are my Pojo Classes
class YearWrapper {
List<YearObject> years;
public List<YearObject> getYears() {
return years;
}
public void setYears(List<YearObject> years) {
this.years = years;
}
@Override
public String toString() {
return "YearWrapper{" +
"years=" + years +
'}';
}
}
class YearObject {
int start;
int end;
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
@Override
public String toString() {
return "YearObject{" +
"start=" + start +
", end=" + end +
'}';
}
}
class KeyValueMapModel {
int position;
List<String> source;
@SerializedName("source_data")
Map<String, YearObject> sourceData;
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public List<String> getSource() {
return source;
}
public void setSource(List<String> source) {
this.source = source;
}
public Map<String, YearObject> getSourceData() {
return sourceData;
}
public void setSourceData(Map<String, YearObject> sourceData) {
this.sourceData = sourceData;
}
@Override
public String toString() {
return "KeyValueMapModel{" +
"position=" + position +
", source=" + source +
", sourceData=" + sourceData +
'}';
}
}
Here is how i am invoking it:
Gson gson = new Gson();
KeyValueMapModel keyValueMapModel = gson.fromJson(yourJsonString, KeyValueMapModel.class);
System.out.println(keyValueMapModel);
Upvotes: 1