I wanna create some Objects in my Realm using "realm.createOrUpdateAllFromJson(type.class, json-array)". But it throws error -> could not map json.
Heres the Json-Array. It's valid.
[
{
"uuid": "99975b79-adac-4baa-a1ab-96c0db23c0ee",
"name": "Joris"
},
{
"uuid": "0",
"name": "Adrian"
},
{
"uuid": "0",
"name": "Oliver"
},
{
"uuid": "0",
"name": "Michael"
},
{
"uuid": "0",
"name": "David"
},
{
"uuid": "0",
"name": "Daniel"
}
]
and heres my Owner-Model.
public class Owner extends RealmObject{
#PrimaryKey
private String uuid; //PK
private String name;
private String token;
private RealmList<Idea> ideas;
private RealmList<Vote> votes;
Here's the code where the error is thrown. its in the 3. line
realm.beginTransaction();
realm.createOrUpdateAllFromJson(Tag.class, tagJson);
realm.createOrUpdateAllFromJson(Owner.class, ownerJson);
realm.commitTransaction();
it's strange how the creating of Tag-Objects works fine..
please help!!
greetings john
You UUID is marked as a #PrimaryKey and you have multiple "uuid"'s with the value 0. This means that you are breaking the primary key constraint of only having one element with each uuid. If you look closely in LogCat you will probably see that Could not map JSON exception mentions this as well.
Related
I have started using retrofit:2.0.2 first time into my new project and I have done with first service call using retrofit but am not sure is it correct way or not here is what i have done
web services response
{
"status": true,
"message": "",
"data": {
"schools": [ { "id": "1", "name": "test 1" }, { "id": "2", "name": "test 12" }],
"referrals": [ { "id": "195", "name": "test 1" }, { "id": "1483", "name": "test 12" }],
"Brands": [ { "id": "195", "name": "test 1" }, { "id": "1483", "name": "test 12" }],
"Teams": [ { "id": "195", "name": "test 1" }, { "id": "1483", "name": "test 12" }],
"positions": [ { "id": "195", "name": "test 1" }, { "id": "1483", "name": "test 12" }],
}
}
created 3 model classes to map above response
public class SimpleObject {
int id;
String name;
// getter setter
}
public class SimpleData {
private List<SimpleObject> schools = new ArrayList<SimpleObject>();
private List<SimpleObject> referrals = new ArrayList<SimpleObject>();
private List<SimpleObject> positions = new ArrayList<SimpleObject>();
private List<SimpleObject> Teams = new ArrayList<SimpleObject>();
private List<SimpleObject> Brands = new ArrayList<SimpleObject>();
// getter, setter
}
public class ResponseData{
boolean status;
String message;
SimpleData data;
// getter setter
}
and then made a service call using Retrofit2
call.enqueue( new Callback<ResponseData>() {
#Override
public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
}
#Override
public void onFailure(Call<ResponseData> call, Throwable t) {
}
and its working fine but a want to insure that is it best way of doing this or can any one suggest the best way of handling such a response without creating multiple model classes for simple data (there should me only one model class "SimpleObject" and other will be list of "SimpleObject")
Please comment or suggest best way to handling response thanks.
The way you currently have it structured looks correct. You need to have one model for every nested level in the JSON response, which you have.
The only other option is simply breaking the object up using keys to pull all of the JSONObjects and JSONArrays, which I do not recommend.
copy and paste your json response in the link given below and download zip file and put it in your project directly.
This link will convert your any kind of json response and convert it into POJO
I have an object like this one:
"choice": {
"000": {
"id": "001",
"label": "test",
"status": "0"
},
"001": {
"id": "001",
"label": "test",
"status": "0"
},
"002": {
"id": "001",
"label": "test",
"status": "0"
},
"003": {
"id": "001",
"label": "test",
"status": "0"
},
"004": {
"id": "001",
"label": "test",
"status": "0"
}
},
How can I parse that object with Gson+Retrofit? Or generate a POJO? There is an easy way of doing this?
Many thanks!
The main idea that all that you have in choice json object is Map:
public class RootObject{
Map <String,ChoiceEntry> choice;
}
public class ChoiceEntry{
String id;
String label;
int status;
}
You can create a POJO by pasting that JSON code into this link: http://pojo.sodhanalibrary.com/.
You posted a snippet that isn't properly formatted. I believe you'll have a multi-class POJO, and that is tricky to work with for certain uses such as listviews.
Let me know how it goes. Retrofit's really nice to use, but extremely annoying to figure out!
There's an addon for intellij/android studio for easy POJO generation.
Disclaimer: I have no affiliation with the project.
I am using volley in my app for network requests.I am trying to use GSON library for JSON parsing and create a list view with that.JSON looks like this.When I try to run the code I got an exception. What is wrong with this and what is missing
[
{
"articles": {
"code": "/par/feed_sub_section",
"name": "News",
"title": "News",
"count": "25",
"page": "1",
"totalPages": "11",
"expiryTime": 100
},
"article": [
{
"title": "title 1",
"articleUrl": "articleUrl 1",
"thumbnail": "thumbnail 1",
"imgWeb": "imgWeb 1",
"imgMob": "imgMob 1",
"lastModified": "lastModified1",
"otherImages": "0",
"video": "false"
},
{
"title": "title2",
"articleUrl": "articleUrl2",
"thumbnail": "thumbnail2",
"imgWeb": "imgWeb2",
"imgMob": "imgMob2",
"lastModified": "lastModified2",
"otherImages": "0",
"video": "false"
},
{
"title": "title3",
"articleUrl": "articleUrl3",
"thumbnail": "thumbnail3",
"imgWeb": "imgWeb3",
"imgMob": "imgMob3",
"lastModified": "lastModified3",
"otherImages": "0",
"video": "false"
},
{
"title": "title4",
"articleUrl": "articleUrl4",
"thumbnail": "thumbnail4",
"imgWeb": "imgWeb4",
"imgMob": "imgMob4",
"lastModified": "lastModified4",
"otherImages": "0",
"video": "false"
}
]
}]
and my classes is like
public class Article {
String title;
String articleUrl;
String thumbnail;
String imgWeb;
String imgMob;
String lastModified;
String otherImages;
String video;}
public class Articles {
String code;
String name;
String title;
String count;
String page;
public class ArticleMainObject {
public Articles articles;
public List<Article> articleList;}
I use volley and try to parse result like this
GsonRequest<ArticleMainObject> myReq = new GsonRequest<ArticleMainObject >(Method.GET,
"http://JSONURL/",
ArticleMainObject .class,
createMyReqSuccessListener(),
createMyReqErrorListener());
When i run this code i got exception like this
com.android.volley.ParseError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 4 column 2
I am stuck at this point.How to iterate through the result.I am new to GSON.So please help me
Have been scratching my head on this one. The JSON Response is a valid one:
{
"MRData": {
"xmlns": "http://ergast.com/mrd/1.4",
"series": "f1",
"url": "http://ergast.com/api/f1/current/2.json",
"limit": "30",
"offset": "0",
"total": "1",
"RaceTable": {
"season": "2014",
"round": "2",
"Races": [
{
"season": "2014",
"round": "2",
"url": "https://en.wikipedia.org/wiki/2014_Malaysian_Grand_Prix",
"raceName": "Malaysian Grand Prix",
"Circuit": {
"circuitId": "sepang",
"url": "http://en.wikipedia.org/wiki/Sepang_International_Circuit",
"circuitName": "Sepang International Circuit",
"Location": {
"lat": "2.76083",
"long": "101.738",
"locality": "Kuala Lumpur",
"country": "Malaysia"
}
},
"date": "2014-03-30",
"time": "08:00:00Z"
}
]
}
}
}
The POJO for the response:
public class ApiResponse {
MRData mrdata;
public class MRData {
String xmlns;
String series;
String url;
String limit;
String offset;
String total;
RaceTable raceTable;
}
}
The apiResponse object is always null. Anyone can point out what is wrong with the POJO object here?
Thanks.
I see a couple of potential issues here:
You are defining a class inside another class; I have never seen it done like this. You might want to separate in two different files.
Your variable names on your POJO should match the variable names on the JSON response exactly. For example:
public class ApiResponse {MRData MRData;}
If you want your POJO's variables to be different than what the JSON sends back, you should use #SerlizedName.
Using Gson on Android, how should I set my Java classes to parse a JSON like this (from Sickbeard API):
{
"data": {
"1": {
"airdate": "2011-09-04",
"name": "Something Wicked This Fae Comes",
"quality": "HD TV",
"status": "Downloaded"
},
"2": {
"airdate": "2011-09-11",
"name": "I Fought the Fae (And the Fae Won)",
"quality": "N/A",
"status": "Wanted"
},
"3": {
"airdate": "2011-09-18",
"name": "Scream a Little Dream",
"quality": "N/A",
"status": "Unaired"
},
"4": {
"airdate": "2011-09-25",
"name": "Episode 4",
"quality": "N/A",
"status": "Unaired"
},
"5": {
"airdate": "",
"name": "Episode 5",
"quality": "N/A",
"status": "Skipped"
}
},
"message": "",
"result": "success"
}
These "1", "2", etc could be a number from 0 to whatever (and it may even be a "specials").
Your classes should look like this
class Response
#Expose #SerializedName("data")
private HashMap<Integer, DataObject> dataList;
#Expose #SerializedName("message")
private String message;
#Expose #SerializedName("result")
private String result;
class DataObject
#Expose #SerializedName("airdate")
private String airdate;
#Expose #SerializedName("name")
private String name;
... //other objects
And getter and setter and so on, now you should be able to parse it. But if you have the possibility to change the api I would suggest you to change the data List to a real json List of objects!!