The question is published on by Tutorial Guruji team.
I am building a Java Android App with Webserver (Raspberry Pi 4 with LAMP) connection for the first time.
There’s a db table “products” which holds all products (Product_id, productName, price). I am retrieving a JSON array though a PHP API.
On App launch, I want to get all products and create Java Objects out of them, so I can dynamically display all products and prices in a Fragment in my app. Note that number and price of products might change, so I need this approach to dynamically create the products.
Since I have never done this before, I am stuck. I know how to get a response in my code, but what I need help with is to turn that response into Java Objects. Any advice how you usually would do that?
Here’s my JSON:
"status": 200, "info": [ { "Product_id": "1", "product": "Coca Cola", "price": "3" }, { "Product_id": "2", "product": "Orange Juice", "price": "2.5" }, { "Product_id": "3", "product": "Apple Juice", "price": "2.5" }, { "Product_id": "4", "product": "Beer", "price": "1.5" }, { "Product_id": "5", "product": "Energy Drink", "price": "3" }, { "Product_id": "6", "product": "Gin Tonic", "price": "5" }, { "Product_id": "7", "product": "Water", "price": "1" }, { "Product_id": "8", "product": "Soda", "price": "1" }, { "Product_id": "9", "product": "Ticket", "price": "30" } ] }
Answer
I just used Gson, as Manuel and Ibrahim suggested. Thank you!