display data in codenameoone using table - json

am making a mobile app using codenameone and i want to display the data from database which i get the following json response using asp.net api
{
"FullNames": "sample string 1",
"PhoneNumber": "sample string 2",
"Date": "sample string 3",
"PickupPoint": "sample string 4",
"Destination": "sample string 5",
"Reason": "sample string 6",
"Time": "sample string 7",
"Booking": "sample string 8"
}

You can parse the JSON using Properties or the JSON parser. Both are supported by the Rest API which returns a map or a PropertyBusinessObject respectively.
The Table is the simple part. You can create it with the Table class.
A super trivial implementation would look something like:
DefaultTableModel model = new DefaultTableModel(
new String[] {"Name", "Value"},
new Object[][] {
{ "Full Name", UserInfo.fullName.get() },
{ "Phone Number", UserInfo.phoneNumber.get() },
...
}
) {
public boolean isEditable(int row, int column) { return column == 1; }
};
Table table = new Table(model);
Form f = new Form("My Table", new BorderLayout());
f.add(CENTER, table);
f.show();
Now notice that we have an automatic UI option that generates a table of business objects for you: https://www.codenameone.com/blog/table-property-mapping.html
But this works for a list of business object, not for an individual object.

Related

Is there a way where all the values from a JSON column can be used as query params to fetch values in db?

I have a lookup table with columns old_item_code, description, new_item_code.
Now my items is an array of objects.
"items":[
{
"code": "OLDCODE1",
"description": "sample description1",
"value": "Sample value1"
},
{
"code": "OLDCODE2",
"description": "Sample Description2",
"value": "Sample Value 2"
}
]
Now I want my items array to be replaced with new item code which has to be queried from the lookup table(postgres).
I want my final result to be like
"items": [
{
"NEWCODE1": "Sample Value 1",
"NewCODE2": "Sample Value 2"
}
]
In PSQL, using where with array will get you the new code. Use a map to get its values.

Nesting arrays in Realm with Swift

I have read several examples on SO on how to store arrays of data to Realm. Still, I am not finding a particularly clear example.
In short, I have a (simplified) JSON as below which I would like to store in Realm. How can I add an array of ingredients to Realm, so that it is contained within an array of menuItems?
{
"menuItems": [
{
"name": "name 1",
"ingredients": ["ingredient 1", "ingredient 2"]
},
{
"name": "name 2",
"ingredients": ["ingredient 1", "ingredient 2", "ingredient 3"
]
}
]
}
I have my realm models set up as such:
class MenuItemsRealm: Object {
#objc dynamic var name: String = ""
var ingredients = List<IngredientItemsRealm>()
}
class IngredientItemsRealm: Object {
#objc dynamic var ingredientItem: String = ""
}
In your JSON, you're stating that a menuItem object has a property/variable called ingredients and it contains an array of String. What you probably want to do is create a array of Objects that contains the specific ingredientItem property/variable.
To exemplify your JSON would be something like this:
{
"menuItems": [
{
"name": "name 1",
"ingredients": [
{
"ingredientItem": "item name"
}
]
}
]
}

Unable to construct java object from json

Can any body tell me Java class structure equivalent to below json:
{
"status": "OK",
"data":
{
"group_id":2758,
"0":
{
"id": "2758-1",
"customid": "1",
"customid1": "",
"customid2": "",
"mobile": "9190********",
"status": "AWAITED_DLR",
},
"1":
{
"id": "2758-2",
"customid": "2",
"customid1": "",
"customid2": "",
"mobile": "9190********",
"status": "AWAITED_DLR",
}
...
}
"message": "Campaign Submitted successfully"
}
Unable to decide the structure of data object as it contains group_id and list of other object.
You can make a first class which represents your structure with :
an Int : group_id
an ArrayList : data, that contains objects of type Item
Then you create the second class Item that represents the structure of "0" and "1".
If you want to keep the label "0", "1" you can make the type of data an Hashmap<String,Item> instead of ArrayList.
You could use a map:
class Data {
int group_id;
Map<Integer, InnerObject> map = new HashMap<>();
}
Data data = new Data();
data.group_id = 2758;
data.map.put(0, innerObject0);
data.map.put(1, innerObject1);

How to parse ObjectId (MongoDB) in JSON that is sent to Grails Server

I have searched many post here but I still couldn't find the answer yet.
Problem:
I want to parse this JSON:
{
"price": {
"currency": {
"class": "com.yintagoka.ad.Currency",
"id": {
"class": "org.bson.types.ObjectId",
"inc": -612166639,
"machine": -1775927134,
"new": false,
"time": 1393682821000,
"timeSecond": 1393682821
},
"code": "USD",
"longName": "United States Dollar",
"name": "USD",
"symbol": null
},
"amount": 100
},
"title": "title",
"description": "desc"
}
to become an Object in my Grails server
class House {
ObjectId id
String name
String title
Price price
// ... many more
}
class Price {
BigDecimal amount
Currency currency
}
class Currency {
ObjectId id
String code
String longName
String name
String symbol
}
Grails did almost all the heavy stuff for me, but it can't generate the ObjectId. So I will have Object Currency with all the property name filled with the value but the id will be null.
How can I tell Grails to render the ObjectId as well, preferably automatically.
Note: I don't want to use String for my id.
I also read a post that I can set it manually by request.JSON, but the problem is that I always get an empty map.
def save(House house) {
print('request = '+request.JSON)
print('house = '+house)
print('params = '+params)
}
out:
request = [:]
house = com.yintagoka.ad.House#217b1ead[id=<null>,title=title,description=desc,price=com.yintagoka.ad.Price#1a1b0107[amount=100,currency=com.yintagoka.ad.Currency#62e07ff4[id=<null>,name=USD,longName=United States Dollar,code=USD,symbol=<null>]]
params = [action:delete, controller:house]

Swagger UI + MVC 4 Web API Get JSON with object name

I am using swagger ui plugin to document my web api. I want to return JSON object like:
{"Person": {
"Id": 1,
"Name": "John",
"address": {
"Street": "ABC",
"City": "Penrith",
"PostCode": 2034,
"State": "NSW"
},
"DOB": "2013-11-11T00:00:00"
}
}
Notice the Person object name.
I can do this with the following code:
public HttpResponseMessage Get(int id)
{
Person person = new Person { Id = 1, Name = "John", DOB = new DateTime(2013, 11, 11), address = new Address { City = "Penrith", State = "NSW", PostCode = 2034, Street = "ABC" } } ;
return Request.CreateResponse(HttpStatusCode.Accepted, new { Person = person });
}
Unfortunately, because the return type is HttpResponseMessage and not Person itself, Swagger just shows model as HttResponseMessage. That's not what I want.
If I change the return type to Person and return a person object, I don't get the the Person object name in the JSON return. That returns only -
{
"Id": 1,
"Name": "John",
"address": {
"Street": "ABC",
"City": "Penrith",
"PostCode": 2034,
"State": "NSW"
},
"DOB": "2013-11-11T00:00:00"
}
Is there a way to return Person but have the JSON string with Person object name?
Simply create a new class which has a Person property, instance it, assign the value to the Person property and return this object.
In this way, the JSON will look as expected.
If you don't want to create a new class, try using an anonymous type, like this:
// If you have this object
var MyPerson = ...;
// Return this from Web API
return new { Person = MyPersons };
(I don't know if this last option will work for you)