I'm using volley and gson libs in my Android app which main purpose is to call to the server API and retrieve some data.
Everything works well, expect for nested JSON object data. I can't retrieve information (lat, lng) which are inside position object.
It throws an error:
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 99 path $.position and points out to deliveryResponse method.
Example of my JSON:
[{"number":18,"name":"John","address":"John Street.","position":{"lat":12.68300406,"lng":45.28001237},"status":"OPEN"},{"number":18,"name":"John","address":"John Street.","position":{"lat":12.68300406,"lng":45.28001237},"status":"OPEN",}]
deliveryResponse method:
#Override
protected void deliverResponse(JSONArray jsonArray) {
if (mListener != null && jsonArray != null) {
List<T> responseArray = new ArrayList<T>();
for (int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject entry = jsonArray.getJSONObject(i);
T parsedResponse = new Gson().fromJson(entry.toString(), mClass);
if (parsedResponse != null) {
responseArray.add(parsedResponse);
}
} catch (JSONException e) {
Log.d(TAG, "Error parsing JSON Object: " + e.getMessage());
mListener.onResponse(jsonArray);
}
}
mListener.onGsonResponse(responseArray);
}
}
Object class:
public class Object {
private int number;
private String name;
private String address;
private List<PositionObj> position;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public List<PositionObj> getPosition() {
return position;
}
public void setPosition(List<PositionObj> position) {
this.position = position;
}
PositionObj class:
public class PositionObj {
private int lat;
private int lng;
public int getLat() {
return lat;
}
public void setLat(int lat) {
this.lat = lat;
}
public int getLng() {
return lng;
}
public void setLng(int lng) {
this.lng = lng;
}
What do you sugggest?
In your JSON, position is an object and not an array. So, in your class Object(weird name), change private List<PositionObj> position; by private PositionObj position;.
Related
I want to parse this json string but i am getting error, i tried with different way but it is not working
My error
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of arrest_warrants out of START_ARRAY token
{
"arrest_warrants": [
{
"issuing_country_id": "CO",
"charge": "dfdfd",
"charge_translation": null
}
],
"weight": 0,
"forename": "MOISES",
"date_of_birth": "1965/06/02",
"entity_id": "2021/40054"
}
Parse calling: i am getting error on below line
Interpoldetails intterdt = restTemplate.getForObject("xxxurl"+id, Interpoldetails.class);
My class
#JsonIgnoreProperties(ignoreUnknown = true)
public class Interpoldetails {
private Integer weight;
private String date_of_birth;
private String entity_id;
private String name;
private arrest_warrants arrest_warrants;
private String[] languages_spoken_ids;
private Integer height;
private Integer sex_id;
private String country_of_birth_id;
private String place_of_birth;
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
public String getDate_of_birth() {
return date_of_birth;
}
public void setDate_of_birth(String date_of_birth) {
this.date_of_birth = date_of_birth;
}
public String getEntity_id() {
return entity_id;
}
public void setEntity_id(String entity_id) {
this.entity_id = entity_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getLanguages_spoken_ids() {
return languages_spoken_ids;
}
public void setLanguages_spoken_ids(String[] languages_spoken_ids) {
this.languages_spoken_ids = languages_spoken_ids;
}
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
public Integer getSex_id() {
return sex_id;
}
public void setSex_id(Integer sex_id) {
this.sex_id = sex_id;
}
public String getCountry_of_birth_id() {
return country_of_birth_id;
}
public void setCountry_of_birth_id(String country_of_birth_id) {
this.country_of_birth_id = country_of_birth_id;
}
public String getPlace_of_birth() {
return place_of_birth;
}
public void setPlace_of_birth(String place_of_birth) {
this.place_of_birth = place_of_birth;
}
public arrest_warrants getArrest_warrants() {
return arrest_warrants;
}
public void setArrest_warrants(arrest_warrants arrest_warrants) {
this.arrest_warrants = arrest_warrants;
}
}
My inner class
#JsonIgnoreProperties(ignoreUnknown = true)
public class arrest_warrants {
private String issuing_country_id;
private String charge;
public String getIssuing_country_id() {
return issuing_country_id;
}
public void setIssuing_country_id(String issuing_country_id) {
this.issuing_country_id = issuing_country_id;
}
public String getCharge() {
return charge;
}
public void setCharge(String charge) {
this.charge = charge;
}
}
it should be arrest_warrants[] not String[]
As arrest_warrants is an array of objects you can create arrest_warrant object and in Interpoldetails class, use private List<arrest_warrant> arrest_warrants
The result variable contains corrected parsed JSON.
But after deserialization List contains correct amount of items but all of them are empty.
How to fix it?
Gson gson = new Gson();
List<UnitView> unitViews = new ArrayList<UnitView>();
// https://stackoverflow.com/questions/5554217/google-gson-deserialize-listclass-object-generic-type
Type typeToken = new TypeToken<List<UnitView>>() { }.getType();
unitViews = gson.fromJson(result,typeToken);
Even if I do like
UnitView[] unitViews = gson.fromJson(result, UnitView[].class);
The fields of items are empty as well.
UnitView
public class UnitView implements Serializable {
public String id ;
public String name ;
public String description ;
public String deviceTypeName ;
public String categoryID ;
public String lastOnline ;
public String latitude ;
public String longitude ;
public String atTime ;
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public String getDeviceTypeName() {
return deviceTypeName;
}
public String getCategoryID() {
return categoryID;
}
public String getLastOnline() {
return lastOnline;
}
public String getLatitude() {
return latitude;
}
public String getLongitude() {
return longitude;
}
public String getAtTime() {
return atTime;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setDescription(String description) {
this.description = description;
}
public void setDeviceTypeName(String deviceTypeName) {
this.deviceTypeName = deviceTypeName;
}
public void setCategoryID(String categoryID) {
this.categoryID = categoryID;
}
public void setLastOnline(String lastOnline) {
this.lastOnline = lastOnline;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public void setAtTime(String atTime) {
this.atTime = atTime;
}
}
JSON DATA
[{"ID":"294","Name":"Foton Tunland № F110","Description":null,"DeviceTypeName":"Техника ТО","CategoryID":"18","LastOnline":"19.12.2017 20:38:04","Latitude":"11,40119","Longitude":"11,42403","AtTime":"19.12.2017 20:38:04"},{"ID":"295","Name":"DML LP1200 № 9793","Description":null,"DeviceTypeName":"Буровой станок дизельный","CategoryID":"15","LastOnline":null,"Latitude":null,"Longitude":null,"AtTime":null}]
Ok , the problem is that the parser is case-sensitive, you can change the name of your attributes to match the name of the json value of you could use the SerializedName annotation like this:
#SerializedName("ID")
public String id ;
#SerializedName("Name")
public String name ;
#SerializedName("Description")
public String description;
...
or
public String ID ;
public String Name ;
public String Description ;
...
I think you're having this problem because of null values in your json.
Check it. Source
I want to get data from the string send by Node.js with that code:
User[] user2 = mapper.readValue(resultPayload, User[].class);
My User class:
public class User {
private double lat;
private double lon;
public User() {
}
public User(double lat, double lon) {
this.lat = lat;
this.lon = lon;
}
public double getLocationLat(){return lat;}
public void setLocationLat(double lat){this.lat = lat;}
public double getLocationLon(){return lon;}
public void setLocationLon(double lon){this.lon = lon;}
}
But I get such an warning:
WARNING: could not load Java7 Path class
/com.amazon.mysampleapp W/System.err: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.Double out of START_OBJECT token
My JSON string contains:
[{"locationLon":{"N":"16.946397721767426"},"locationLat":{"N":"52.447558225994904"}},{"locationLon":{"N":"16.88841037452221"},"locationLat":{"N":"52.44599882989592"}},{"locationLon":{"N":"16.94861490279436"},"locationLat":{"N":"52.44514319230585"}}]
Step 1: Location.java
import com.fasterxml.jackson.annotation.JsonProperty;
public class Location {
private double N;
public Location() {
}
public Location(double n) {
N = n;
}
#JsonProperty("N")
public double getN() {
return N;
}
public void setN(double n) {
N = n;
}
#Override
public String toString() {
return "N=" + N;
}
}
Step 2: User.java
public class User {
private Location locationLat;
private Location locationLon;
public User() {
}
public User(Location locationLat, Location locationLon) {
super();
this.locationLat = locationLat;
this.locationLon = locationLon;
}
public Location getLocationLat() {
return locationLat;
}
public void setLocationLat(Location locationLat) {
this.locationLat = locationLat;
}
public Location getLocationLon() {
return locationLon;
}
public void setLocationLon(Location locationLon) {
this.locationLon = locationLon;
}
#Override
public String toString() {
return "User [locationLat=" + locationLat + ", locationLon=" + locationLon + "]";
}
}
Step 3:
String resultPayload=new String(Files.readAllBytes(new File("D:/test3.json").toPath()));
ObjectMapper mapper = new ObjectMapper();
User[] users = mapper.readValue(resultPayload, User[].class);
System.out.println(Arrays.toString(users));
Please consider making all of the above changes and see the result.
This question already has answers here:
Java - IndexOutOfBoundsException: Index: 1, Size: 0
(1 answer)
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed last year.
int i = 0; //used for getting item position
for (Iterator<CustomObject> iterator = mAdapter.getItemsData().iterator(); iterator.hasNext();) {
if (mAdapter.getItemsData().get(i).getPriceTier() != 1) { //if statement throws error
// Remove the current element from the iterator and the list.
iterator.remove();
mAdapter.removeFromItemsData(i);
}
i++;
}
Using this stackoverflow answer, I iterate through my arraylist of CustomObject. Custom object is just a POJO class with setters and getters. The setters and getters have different data types, such as int, String, double etc.
mAdapter.getItemsData returns the araylist from the adapter class.
//method just notifies the RecyclerView that an item was removed.
public void removeFromItemsData(int position){
notifyItemRemoved(position);
}
Here is the exception
java.lang.IndexOutOfBoundsException: Invalid index 18, size is 18
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308) at
com.test.TestActivity$5.onClick(TestActivity.java:280) at
android.view.View.performClick(View.java:4780) at
android.view.View$PerformClick.run(View.java:19866) at
android.os.Handler.handleCallback(Handler.java:739) at
android.os.Handler.dispatchMessage(Handler.java:95) at
android.os.Looper.loop(Looper.java:135) at
android.app.ActivityThread.main(ActivityThread.java:5254) at
java.lang.reflect.Method.invoke(Native Method) at
java.lang.reflect.Method.invoke(Method.java:372) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
CustomObject class
public class CustomObject{
private String name;
private int distance;
private String category;
private String id;
private double rating;
private String ratingColor;
private String prefix;
private String suffix;
private int priceTier;
private String currency;
private String phone;
private String twitter;
private String url;
private String menu;
String address;
String city;
String state;
String postalCode;
double lat;
double lng;
String review;
int reviewCount;
String firstName;
String lastName;
String userIconPrefix;
String userIconSuffix;
String secondsReview;
public CustomObject() {
this.name = "";
this.distance = 0;
this.setCategory("");
this.id = "";
this.rating = 0;
this.ratingColor = "";
this.prefix = "";
this.suffix = "";
this.priceTier = 0;
this.currency = "";
this.phone = "";
this.twitter = "";
this.url = "";
this.menu = "";
this.address = "";
this.city = "";
this.state = "";
this.postalCode = "";
this.lat = 0;
this.lng = 0;
this.review = "";
this.reviewCount = 0;
this.firstName = "";
this.lastName = "";
this.userIconPrefix = "";
this.userIconSuffix = "";
this.secondsReview = "";
}
public void setSecondsReview(String secondsReview){
this.secondsReview = secondsReview;
}
public String getSecondsReview(){
return secondsReview;
}
public void setUserIconSuffix(String userIconSuffix){
this.userIconSuffix = userIconSuffix;
}
public String getUserIconSuffix(){
return userIconSuffix;
}
public void setUserIconPrefix(String userIconPrefix){
this.userIconPrefix = userIconPrefix;
}
public String getUserIconPrefix(){
return userIconPrefix;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getFirstName(){
return firstName;
//todo if String firstNAme == null, then return "a foursquare user"
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public String getLastName(){
return lastName;
}
public void setReviewCount(int reviewCount){
this.reviewCount = reviewCount;
}
public int getReviewCount(){
return reviewCount;
}
public void setReview(String review){
this.review = review;
}
public String getReview(){
return review;
}
public void setLng(double lng){
this.lng = lng;
}
public double getLng(){
return lng;
}
public void setLat(double lat){
this.lat = lat;
}
public double getLat(){
return lat;
}
public void setPostalCode(String postalCode){
this.postalCode = postalCode;
}
public String getPostalCode(){
return postalCode;
}
public void setState(String state){
this.state = state;
}
public String getState(){
return state;
}
public void setCity(String city){
this.city = city;
}
public String getCity(){
return city;
}
public void setAddress(String address){
this.address = address;
}
public String getAddress(){
return address;
}
public void setMenuUrl(String menu){
this.menu = menu;
}
public String getMenuUrl(){
return menu;
}
public void setUrl(String url){
this.url = url;
}
public String getUrl(){
return url;
}
public void setTwitter(String twitter){
this.twitter = twitter;
}
public String getTwitter(){
return twitter;
}
public void setPhone(String phone){
this.phone = phone;
}
public String getPhone(){
return phone;
}
public String getCurrency(){
return currency;
}
public void setCurrency(String currency){
this.currency = currency;
}
public int getPriceTier(){
return priceTier;
}
public void setPriceTier(int priceTier){
this.priceTier = priceTier;
}
public String getSuffix(){
return suffix;
}
public void setSuffix(String suffix){
this.suffix = suffix;
}
public String getPrefix(){
return prefix;
}
public void setPrefix(String prefix){
this.prefix = prefix;
}
public double getRating(){
if(rating > 0){
return rating;
}
return 0; //TODO display symbol if rating not found
}
public void setRating(double rating){
this.rating = rating;
}
public Integer getDistance() {
if (distance >= 0) {
return distance;
}
return 0; //TODO display symbol if distance not found, like N/A
}
public void setDistance(int distance) {
this.distance = distance;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
}
How data is added
In onCreate of activity, I instantiate Recyclerview. RecyclerView takes an arraylist, so I pass an empty one.
List listTitle = new ArrayList(); //empty arraylist
mAdapter = new Fragment1Adapter(listTitle);
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mAdapter);
Later on in an AsyncTask, I add the items to the Adapter.
for (int i = 0; i < jsonArray.length(); i++) {
CustomObject poi = new CustomObject ();
if (jsonArray.getJSONObject(i).has("venue")) {
poi.setName(jsonArray.getJSONObject(i).getJSONObject("venue").getString("name"));
poi.setId(jsonArray.getJSONObject(i).getJSONObject("venue").getString("id"));
}
mAdapter.addItem(poi);
}
Here is the addItem class
Declared at top of adapter class private final List<FoursquareVenue> itemsData;
public void addItem(FoursquareVenue data) {
itemsData.add(data);
notifyItemInserted(itemsData.size() - 1);
}
Edit
Since you our chat and your edits:
You need to add and remove the items to your ArrayList and them notify adapter of change.
CustomObject poi = new CustomObject ();
if (jsonArray.getJSONObject(i).has("venue")) {
poi.setName(jsonArray.getJSONObject(i).getJSONObject("venue").getString("name"));
poi.setId(jsonArray.getJSONObject(i).getJSONObject("venue").getString("id"));
}
listTitle.addItem(poi);// Add to ListArray
Then loop through your ArrayList to remove items.
You've just muddled yourself up a bit in how you are trying to access your data.
There is a problem that you are creating an iterator of CustomObjects, and parsing it to the data type of mAdapter.getItemsData().
Removing the element from the iterator should remove it from the list.
for (Iterator<CustomObject> iterator = mAdapter.iterator(); iterator.hasNext();) {
CustomObject itemsData = iterator.next();
if (itemsData.getItemsData().get(i).getPriceTier() != 1) { //if statement throws error
// Remove the current element from the iterator and the list.
iterator.remove();
}
i++;
}
CustomObject itemsData = iterator.next();
Let me know if this helps.
I am parsing the JSON array successfully. But I have a String which has numbers. So I want to sort all the data according to the numbers. I had been checked so many examples but I couldn't implement them in my code.So please help me.
Here is my code. here the "count" is the, string threw which I want to sort the data.
a.java
#Override
protected Void doInBackground(Void... params) {
ServiceHandler serviceHandler = new ServiceHandler();
String jsonStr = serviceHandler.makeServiceCall(
JSONUrl.categoriesUrl, ServiceHandler.GET);
Log.d("Response Categories:", ">" + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
categoriesJSONArray = jsonObj
.getJSONArray(JSONUrl.TAG_DATA);
for (int i = 0; i < categoriesJSONArray.length(); i++) {
JSONObject c = categoriesJSONArray.getJSONObject(i);
GridViewItem gridCategoriesItem = new GridViewItem();
gridCategoriesItem.setSlug(c
.getString(JSONUrl.TAG_CATEGORIES_SLUG));
gridCategoriesItem.setImage(c
.getString(JSONUrl.TAG_CATEGORIES_IMAGE));
gridCategoriesItem.setCount(c
.getString(JSONUrl.TAG_CATEGORIES_COUNT));
mGridArrayCategories.add(gridCategoriesItem);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.d("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
GridViewItem.java
public class GridViewItem {
String image;
String slug;
String count;
String name;
public GridViewItem() {
super();
}
public GridViewItem(String image, String slug, String count,
String name) {
super();
this.image = image;
this.slug = slug;
this.count = count;
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Instead of using bubble sort which is o(n2).
You can use the native sort from the Collection class
example:
Collections.sort(mGridArrayCategories, new Comparator<GridViewItem>() {
public int compare(GridViewItem s, GridViewItem s2) {
return Integer.parseInt(s2.getCount()) - Integer.parseInt(s.getCount()); //this will sort your arrayList in decending order
}
});
You need to parse your string to int so it will be sorted according to the count
You can use Collection.sort().
for sorting any kind of ArrayLsit Object.
Collections.sort(listOfStringArrays,new Comparator<String[]>() {
public int compare(String[] strings, String[] otherStrings) {
return strings[1].compareTo(otherStrings[1]);
}
});