如何在Java Android应用中使用从Web服务器检索到的JSON来动态创建和使用对象

huangapple go评论61阅读模式
英文:

How to use JSON retrieved from Webserver in Java Android App to create and use Objects dynamically

问题

我第一次正在构建一个带有Web服务器连接(使用Raspberry Pi 4上的LAMP)的Java Android应用程序。

有一个名为“products”的数据库表,其中包含所有产品(Product_id、productName、price)。我通过一个PHP API检索JSON数组。

在应用程序启动时,我想获取所有产品并将其创建为Java对象,这样我就可以在应用程序的一个片段中动态显示所有产品和价格。请注意,产品的数量和价格可能会更改,因此我需要这种方法来动态创建产品。

由于我以前从未做过这个,我陷入了困境。我知道如何在代码中获取响应,但我需要帮助将该响应转换为Java对象。您通常会如何处理?以下是我的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”
    }
]
英文:

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"
    }
    ]
}

答案1

得分: 0

我刚刚使用了Gson,就像Manuel和Ibrahim建议的那样。谢谢!

英文:

I just used Gson, as Manuel and Ibrahim suggested. Thank you!

huangapple
  • 本文由 发表于 2020年9月8日 15:40:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63789257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定