I am trying to get the value of class in the following JSON:
{
"battlegroup": "Misery",
"class": 1,
"race": 4,
"gender": 0
}
I access the other fields (battlegroup, race etc.) with:
WoWDetails info = gson.fromJson(response, WoWDetails.class);
raceID = info.race;
WoWDetails is as following:
class WoWDetails {
// character details
public String battlegroup;
public Integer achievementPoints;
public Integer race;
public Integer class;
}
But if I try WoWDetails.class it's giving me an error saying "Unknown class: info". Which makes sense because info is not a class.
Is there a way around this? Can I escape the word "class" in any way possible?
The name class is not editable, since it's not my API.
You can map the class field to JSON key using #SerializedName
WowDetails.java
public class WoWDetails {
String battlegroup;
#SerializedName("class")
int className;
int race;
int gender;
}
Set your Package name
package com.nonprofit.nonprofit;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class WoWDetails {
#SerializedName("battlegroup")
#Expose
private String battlegroup;
#SerializedName("class")
#Expose
private Integer _class;
#SerializedName("race")
#Expose
private Integer race;
#SerializedName("gender")
#Expose
private Integer gender;
public String getBattlegroup() {
return battlegroup;
}
public void setBattlegroup(String battlegroup) {
this.battlegroup = battlegroup;
}
public Integer getClass_() {
return _class;
}
public void setClass_(Integer _class) {
this._class = _class;
}
public Integer getRace() {
return race;
}
public void setRace(Integer race) {
this.race = race;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
}
You can get your details like below
WoWDetails info = gson.fromJson(response, WoWDetails.class);
info.getGender();
info.getRace();
Related
I'm using GitHub API to show in my application the most starred repository and their names and avatar and description in recyclerView but when I lunch the app everything working but the avatar_url and login return Null.
this is a JSON from Github API
https://api.github.com/search/repositories?q=created:%3E2019-10-01&sort=stars&order=desc
I tried this :
client class:
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class Client {
public static final String BASE_URL="https://api.github.com";
public static Retrofit retrofit=null;
public static Retrofit getClient()
{
if(retrofit==null)
{
retrofit=new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
}
return retrofit;
}
}
Service Class:
package com.example.gethubapi.api;
import com.example.gethubapi.model.ItemResponse;
import retrofit2.Call;
import retrofit2.http.GET;
public interface Service {
#GET("/search/repositories?q=created:>2017-10-22&sort=stars&order=desc&page=2")
Call<ItemResponse> getItems();
}
Item class
here is the problem if u checked the jSON file in link above you will find a child object from item called owner and i cant select the name of avatar_url and owner name directly
import com.google.gson.annotations.SerializedName;
public class Item {
#SerializedName("avatar_url")
#Expose
private String avatarUrl;
#SerializedName("name")
#Expose
private String name;
#SerializedName("description")
#Expose
private String description;
#SerializedName("login")
#Expose
private String owner;
#SerializedName("stargazers_count")
#Expose
private int stargazers;
public Item(String avatar_url,String name,String description,String owner,int stargazers )
{
this.avatarUrl=avatar_url;
this.name=name;
this.description=description;
this.owner=owner;
this.stargazers=stargazers;
}
public String getAvatarUrl()
{
return avatarUrl;
}
public String getName()
{
return name;
}
public String getDescription()
{
return description;
}
public String getOwner()
{
return owner;
}
public int getStargazers()
{
return stargazers;
}
}
Looking at the JSON response, the Owner object is part of the Item object. meaning it's nested in the Item object.
public class Owner {
#SerializedName("avatar_url")
#Expose
private String avatarUrl;
#SerializedName("login")
#Expose
private String login;
public Owner(){}
public void setAvatarUrl(String avatar_URL){
this.avatarUrl = avatar_URL;
}
pubic String getAvatarUrl(){
return avatarUrl;
}
public String getLogin(){
return login;
}
public void setLogin(String login){
this.login = login;}
}
public class Item {
#SerializedName("name")
#Expose
private String name;
#SerializedName("description")
#Expose
private String description;
#SerializedName("owner")
#Expose
private Owner owner;
#SerializedName("stargazers_count")
#Expose
private int stargazers;
.........
}
Since the login and avatar_url is under owner object.
You need to create a separate class for owner object , just like you did for a single item.
And don't forget to mention the object class in your Item class.
In my case the stargazer spelling was wrong, just correct the spelling
example:- from
stargazer_count
to
stargazers_count.
I'm trying to parse a JSON like this
{
"meta": {
"limit": 20,
"next": null,
"offset": 0,
"previous": null,
"total_count": 1
},
"objects": [
{
"customer_id": "some-customer-id",
"id": 5,
"is_active": true,
"product_code": "some-product-code",
"resource_uri": "/api/v1/aws-marketplace/5/",
"support_subscription_id": "22"
}
]
}
generated for http://www.jsonschema2pojo.org I got his Java clases
package com.greenqloud.netappstats.collector.metrics.gqconsole.api;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class AwsMarketplace {
#SerializedName("meta")
#Expose
private Meta meta;
#SerializedName("objects")
#Expose
private List<Object> objects = null;
public Meta getMeta() {
return meta;
}
public void setMeta(Meta meta) {
this.meta = meta;
}
public List<Object> getObjects() {
return objects;
}
public void setObjects(List<Object> objects) {
this.objects = objects;
}
}
Meta class
package com.greenqloud.netappstats.collector.metrics.gqconsole.api;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Meta {
#SerializedName("limit")
#Expose
private int limit;
#SerializedName("next")
#Expose
private String next;
#SerializedName("offset")
#Expose
private int offset;
#SerializedName("previous")
#Expose
private String previous;
#SerializedName("total_count")
#Expose
private int totalCount;
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public String getNext() {
return next;
}
public void setNext(String next) {
this.next = next;
}
public int getOffset() {
return offset;
}
public void setOffset(int offset) {
this.offset = offset;
}
public String getPrevious() {
return previous;
}
public void setPrevious(String previous) {
this.previous = previous;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
}
Object class
package com.greenqloud.netappstats.collector.metrics.gqconsole.api;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Object {
#SerializedName("customer_id")
#Expose
private String customerId;
#SerializedName("id")
#Expose
private int id;
#SerializedName("is_active")
#Expose
private boolean isActive;
#SerializedName("product_code")
#Expose
private String productCode;
#SerializedName("resource_uri")
#Expose
private String resourceUri;
#SerializedName("support_subscription_id")
#Expose
private String supportSubscriptionId;
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public boolean isIsActive() {
return isActive;
}
public void setIsActive(boolean isActive) {
this.isActive = isActive;
}
public String getProductCode() {
return productCode;
}
public void setProductCode(String productCode) {
this.productCode = productCode;
}
public String getResourceUri() {
return resourceUri;
}
public void setResourceUri(String resourceUri) {
this.resourceUri = resourceUri;
}
public String getSupportSubscriptionId() {
return supportSubscriptionId;
}
public void setSupportSubscriptionId(String supportSubscriptionId) {
this.supportSubscriptionId = supportSubscriptionId;
}
}
But when trying to deserialise with this func
Type localVarReturnType = new TypeToken<List<InstanceCreatorForAwsMarketplace>>(){}.getType();
ApiResponse<List<InstanceCreatorForAwsMarketplace>> responseFromApiCleint = apiClient.execute(newCall, localVarReturnType);
I get the following error:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
Any idea what might be the issue? This looks like a fairly simple json that the rest service returns, but I have not been able to get it work, any help would be greatly appreciated.
Right after I had written the question, I found out the answer, of course the top level class should not be a list, just the class it self. But I have a secondary question, can the three mapper classes be mounted into one class, just a cosmetic issue?
This question already has answers here:
#RequestBody is getting null values
(17 answers)
Closed 3 years ago.
I wrote a controller for receiving a post request that posts json however when i try to access the object fields it returns null. The code for the Controller is below
package com.example;
import com.example.Services.ZesaServices;
import com.example.models.Zesa.ZesaRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class MoneyResource {
#RequestMapping("/services")
public String getServices(){
return "We do everything";
}
#PostMapping(value="/zesa")
public String payZesa(#RequestBody ZesaRequest newZesaRequest){
Logger log = LoggerFactory.getLogger(YomoneyResource.class);
log.info("Json Object parsed works eg: "+newZesaRequest.getMeterNumber());
return newZesaRequest.getMeterNumber().toString();
}
}
And the ZesaRequest Object is as follows
package com.example.models.Zesa;
public class ZesaRequest {
private Double Amount;
private String MeterNumber;
private String PaymentAccountNumber;
private String PaymentAccountDetails;
private int PaymentMethod;
private String MobileNumber;
private String AgentAccountDetails;
private int TransactionType;
public ZesaRequest() {
}
public ZesaRequest(Double amount, String meterNumber, String paymentAccountNumber, String paymentAccountDetails,
int paymentMethod, String mobileNumber, String agentAccountDetails, int transactionType) {
this.Amount = amount;
this.MeterNumber = meterNumber;
this.PaymentAccountNumber = paymentAccountNumber;
this.PaymentAccountDetails = paymentAccountDetails;
this.PaymentMethod = paymentMethod;
this.MobileNumber = mobileNumber;
this.AgentAccountDetails = agentAccountDetails;
this.TransactionType = transactionType;
}
public String getPaymentAccountDetails() {
return PaymentAccountDetails;
}
public void setPaymentAccountDetails(String paymentAccountDetails) {
PaymentAccountDetails = paymentAccountDetails;
}
public String getMobileNumber() {
return MobileNumber;
}
public void setMobileNumber(String mobileNumber) {
MobileNumber = mobileNumber;
}
public Double getAmount() {
return Amount;
}
public void setAmount(Double amount) {
Amount = amount;
}
public String getMeterNumber() {
return MeterNumber;
}
public void setMeterNumber(String meterNumber) {
MeterNumber = meterNumber;
}
public String getPaymentAccountNumber() {
return PaymentAccountNumber;
}
public void setPaymentAccountNumber(String paymentAccountNumber) {
PaymentAccountNumber = paymentAccountNumber;
}
public int getPaymentMethod() {
return PaymentMethod;
}
public void setPaymentMethod(int paymentMethod) {
PaymentMethod = paymentMethod;
}
public String getAgentAccountDetails() {
return AgentAccountDetails;
}
public void setAgentAccountDetails(String agentAccountDetails) {
AgentAccountDetails = agentAccountDetails;
}
public int getTransactionType() {
return TransactionType;
}
public void setTransactionType(int transactionType) {
TransactionType = transactionType;
}
}
My code is printing null When I send the request below
{
"AgentAccountDetails":"example:123",
"MeterNumber":"1110-52-8867",
"PaymentMethod":1,
"Amount":10.50,
"MobileNumber":"0123456789",
"TransactionType":1,
"PaymentAccountNumber":"0123456789",
"PaymentAccountDetails":"null"
}
When I run it it returns an empty String. I am not sure where the problem is I have looked into other examples and they followed a similar pattern and I ran their code and it worked as expected but mine seems to not be converting the json body into the Java object.
You can solve this by using the #JsonProperty annotation above the fields like this:
...
#JsonPropety(value = "Amount")
private Double amount;
...
Or you can rename your properties to start with lowercase letter (both in the VM and in the incoming json), as #OrangeDog suggested in the comments.
Your class defines a property called meterNumber, but your JSON object says MeterNumber instead.
If you must have MeterNumber in your JSON you will need to add #JsonProperty annotations.
It is against both Java and JSON naming conventions to start field names with a capital letter.
As an aside, you can avoid all the boilerplate by using Lombok:
#Data
public class ZesaRequest {
#JsonProperty("Amount")
private Double amount;
#JsonProperty("MeterNumber")
private String meterNumber;
#JsonProperty("PaymentAccountNumber")
private String paymentAccountNumber;
#JsonProperty("PaymentAccountDetails")
private String paymentAccountDetails;
#JsonProperty("PaymentMethod")
private int paymentMethod;
#JsonProperty("MobileNumber")
private String mobileNumber;
#JsonProperty("AgentAccountDetails")
private String agentAccountDetails;
#JsonProperty("TransactionType")
private int transactionType;
}
You also probably don't want "PaymentAccountDetails":"null". It should be either "PaymentAccountDetails":null, or omitted entirely.
I'm using rest assured for Rest api testing with the help of POJO classes like getter and setter methods to set the values but i'm stuck with array list in between rest request,please any one provide proper code to get exact below request to post using rest assured.
Request:
{
"firstName":"SuryaNAMASKARAM",
"lastName":"mangalam",
"mobileNo" :4954758490,
"emailId" :"surya.mangalam#futureretail.in",
"houseNoStreet":"123456",
"buildingName":"",
"landmark":"Nirmala jathara",
"paymentDetail" :
[{"paymentType":"CASH","No":"3519000012","Date":"16-06-2018","amount":"100.00"}]
}
CustomerCreate Class:
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
#SerializedName("firstName")
#Expose
private String firstName;
#SerializedName("lastName")
#Expose
private String lastName;
#SerializedName("mobileNo")
#Expose
private Integer mobileNo;
#SerializedName("emailId")
#Expose
private String emailId;
#SerializedName("houseNoStreet")
#Expose
private String houseNoStreet;
#SerializedName("buildingName")
#Expose
private String buildingName;
#SerializedName("landmark")
#Expose
private String landmark;
#SerializedName("paymentDetail")
#Expose
private List<PaymentDetail> paymentDetail = null;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getMobileNo() {
return mobileNo;
}
public void setMobileNo(Integer mobileNo) {
this.mobileNo = mobileNo;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getHouseNoStreet() {
return houseNoStreet;
}
public void setHouseNoStreet(String houseNoStreet) {
this.houseNoStreet = houseNoStreet;
}
public String getBuildingName() {
return buildingName;
}
public void setBuildingName(String buildingName) {
this.buildingName = buildingName;
}
public String getLandmark() {
return landmark;
}
public void setLandmark(String landmark) {
this.landmark = landmark;
}
public List<PaymentDetail> getPaymentDetail() {
return paymentDetail;
}
public void setPaymentDetail(List<PaymentDetail> paymentDetail) {
this.paymentDetail = paymentDetail;
}
}
PaymentDetails:
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class PaymentDetail {
#SerializedName("paymentType")
#Expose
private String paymentType;
#SerializedName("No")
#Expose
private String no;
#SerializedName("Date")
#Expose
private String date;
#SerializedName("amount")
#Expose
private String amount;
public String getPaymentType() {
return paymentType;
}
public void setPaymentType(String paymentType) {
this.paymentType = paymentType;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
}
Test Class:
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.test.requestpojo.Example;
import com.test.requestpojo.PaymentDetail;
public class TestAPI {
public void setTestData() throws JSONException {
Example example = new Example();
example.setFirstName("Rajesh");
example.setLastName("Kuchana");
example.setMobileNo("3434343434");
example.setEmailId("rajesh.kuchana#futureretail.in");
example.setDateOfBirth("10-10-2018");
example.setGender(1);
example.setHouseNoStreet("Test");
example.setBuildingName("Test");
example.setLandmark("Test");
List<PaymentDetail> data = new ArrayList<PaymentDetail>();
PaymentDetail paymentDetail = new PaymentDetail();
paymentDetail.setAmount("999.00");
data.add(paymentDetail);
JSONObject jsonObject = new JSONObject(example);
JSONArray jsonArray = new JSONArray(data);
jsonArray.put(data);
System.out.println(jsonArray.put(data));
}
In your test class, what you must do is separate the test data creation to a different class and pass that as data provider to your test method. This is from design point of view.
Coming to your problem:
You are already using gson, why are you constructing a JSONObject. Instead do something like below;
Gson gson = new Gson();
String _my_obj = gson.toJson(example);
Hope this is what you are looking for.
JSON String
{
"order":{
"address":{
"city":"seattle"
},
"orderItem":[
{
"itemId":"lkasj",
"count":2
},
{
"itemId":"ldka",
"count":3
}
]
}
}
Order Class
public class Order {
private OrderItem[] orderItems;
private CustomerAddress address;
Order(OrderItem[] orderItems, CustomerAddress address ) {
this.orderItems = orderItems;
this.address = address;
}
public OrderItem[] getOrderItems() {
return orderItems;
}
public void setOrderItems(OrderItem[] orderItems) {
this.orderItems = orderItems;
}
public CustomerAddress getAddress() {
return address;
}
public void setAddress(CustomerAddress address) {
this.address = address;
}
}
My OrderItem class
package com.cbd.backend.model;
import org.springframework.data.annotation.Id;
public class OrderItem {
#Id
private String id;
private String itemId;
private String count;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
unit Test that blows up
public String getItemId() {
return itemId;
}
public void setItemId(String itemId) {
this.itemId = itemId;
}
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
}
Unit test to demonstrate issue
package com.cbd.backend.model;
import com.google.gson.Gson;
import org.junit.Test;
import static org.junit.Assert.*;
public class OrderTest {
Gson gson = new Gson();
#Test
public void gsonToOrder() {
Order order = gson.fromJson( a, Order.class );
assertNotNull(order);
assertNotNull(order.getOrderItems()[0]);
}
private final String a = "{ \"order\": { \"address\": { \"city\": \"seattle\" },\"orderItem\":[{ \"itemId\":\"lkasj\", \"count\":2 }, { \"itemId\":\"ldka\", \"count\":3 } ] } }";
}
Should I be using something other than gson or am i constructing this incorrectly
There are two problems in your code:
The root element of your JSON is "order", but the class does not have a property with this name. Try changing you model or just removing the element from the JSON.
There is a mismatch in the name of the "orderItem" property. It is plural in the class, but singular in the JSON.
To sum it up, the following JSON will work without any changes to the code.
{
"address":{
"city":"seattle"
},
"orderItems":[
{
"itemId":"lkasj",
"count":2
},
{
"itemId":"ldka",
"count":3
}
]
}
Also, "count" as it appears in the JSON seems to be numeric, so you might want to change the type of OrderItem.count to int or java.lang.Integer.