简体   繁体   English

如何解析JSON文件并使用它来创建类对象?

[英]How do you parse a JSON file and use it to create a class object?

I was working around with JSON files that I could use in my program. 我正在处理我可以在我的程序中使用的JSON文件。 However, I was a bit confused on how to use the library. 但是,我对如何使用库感到有点困惑。 For example, this is the following JSON file I am trying to parse here: 例如,这是我试图在这里解析的以下JSON文件:

{
"shoes": [
    {
        "shoeName": "Shoe",
        "shoePrice": "120",
        "brand": "Shoe",
        "typeOfShoes": "Running",
        "style": "Cool",
        "Color": [
            "Blue",
            "Green",
            "Pink"
        ],
        "Sizes": [
            "W5/M3.5",
            "W5.5/M4"
        ],
        "Description": "The Shoe SE features sleek lines and a sheer upper that combine classic Air Max elements into a lightweight, comfortable and versatile icon. Together with its smart toe-down profile and extra lift, the shoe offers an ever-bigger expression..",
        "shipping": "0",
        "tax": "0",
        "sub-total": "0",
        "review": "4.5",
        "images": [
            "https://c.static-nike.com/a/images/t_PDP_864_v1/f_auto,b_rgb:f5f5f5/ds8ojj70wtpthbzadaft/air-max-dia-se-shoe-WCG8t5.jpg",
            "https://c.static-nike.com/a/images/t_PDP_864_v1/f_auto,b_rgb:f5f5f5,q_80/n5txnsyb21v5zhruxfer/air-max-dia-se-shoe-WCG8t5.jpg"
        ],
        "totalUsers": "60",
        "totalRaffles": "80",
        "clientID_Paypal": "",
        "clientSecret_Paypal": "",
        "isSold": "false"
    },
    {
        "shoeName": "Empty Shoe",
        "shoePrice": "",
        "brand": "",
        "typeOfShoes": "",
        "style": "",
        "Color": [
        ],
        "Sizes": [
        ],
        "Description": "",
        "shipping": "",
        "tax": "",
        "sub-total": "",
        "review": "",
        "images": [
        ],
        "totalUsers": "",
        "totalRaffles": "",
        "clientID_Paypal": "",
        "clientSecret_Paypal": "",
        "isSold": "false"
    },
    {
        "shoeName": "Empty Shoe1",
        "shoePrice": "",
        "brand": "",
        "typeOfShoes": "",
        "style": "",
        "Color": [
        ],
        "Sizes": [
        ],
        "Description": "",
        "shipping": "",
        "tax": "",
        "sub-total": "",
        "review": "",
        "images": [
        ],
        "totalUsers": "",
        "totalRaffles": "",
        "clientID_Paypal": "",
        "clientSecret_Paypal": "",
        "isSold": "false"
    }
]

}

The JSON file is thoroughly simple. JSON文件非常简单。 Inside, there is an array of objects called "shoes" which each have their own shoeName, shoePrice, brand, etc. However, how would I be able to create an array "Shoe" objects by retrieving each of the values in the JSON file? 在里面,有一系列称为“鞋子”的对象,每个对象都有自己的shoeName,shoePrice,品牌等。但是,如何通过检索JSON文件中的每个值来创建数组“Shoe”对象? ?

My Shoe.java: 我的Shoe.java:

import java.awt.Image;

public class Shoe {

    public int shoePrice;
    public int shipping;
    public int tax;
    public int subtotal;
    public int totalUsers;

    public double review;

    public int totalRaffles;
    public int rafflesBought;

    public String shoeName;
    public String style;
    public String typeOfShoes;
    public String brand;

    public Image[] images;

    public String name;
    public String description;
    public String[] colors;
    public String[] sizes;

    public boolean isSold;

    public Shoe(int shoePrice, int shipping, int tax, int subtotal, double review,

            int totalRaffles, int rafflesBought,

            String shoeName, String style, String typeOfShoes, String brand,

            Image[] images,

            String description, String[] colors, String[] sizes,

            boolean isSold) {

        this.shoePrice = shoePrice;
        this.shipping = shipping;
        this.tax = tax;
        this.subtotal = subtotal;
        this.review = review;
        this.totalRaffles = totalRaffles;
        this.rafflesBought = rafflesBought;
        this.sizes = sizes;
        this.shoeName = shoeName;
        this.style = style;
        this.typeOfShoes = typeOfShoes;
        this.images = images;
        this.description = description;
        this.colors = colors;
        this.isSold = isSold;
        this.brand = brand;

    }

}

What I have tried: 我尝试过的:

I have tried to define a Shoe array and tried initializing it using a for loop to retrive the values, make a Shoe, and add it to the array, however, I am confused on how you would be able to program this. 我试图定义一个Shoe数组并尝试使用for循环初始化它来检索值,制作一个Shoe,然后将它添加到数组中,但是,我很困惑你如何能够编程。

Use a library like GSON to convert JSON to Object. 使用像GSON这样的库将JSON转换为Object。

In pom.xml, add dependency to GSON: 在pom.xml中,向GSON添加依赖项:

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
 <version>2.8.5</version>
</dependency>

Since your JSON returns String even for integers and doubles, this is the suitable definition for your Shoe Class. 由于您的JSON甚至为整数和双精度返回String,因此这是您的鞋类的合适定义。

public class Shoe
{
    public String shoeName;

    public String shoePrice;

    public String brand;

    public String typeOfShoes;

    public String style;

    public String[] Color;

    public String[] Sizes;

    public String Description;

    public String shipping;

    public String tax;

    public String subtotal;

    public String review;

    public String[] images;

    public String totalUsers;

    public String totalRaffles;

    public String clientID_Paypal;

    public String clientSecret_Paypal;

    public boolean isSold;

}

Since 'shoes" is the parent level object, you need to define it as well 由于'shoes'是父级对象,因此您也需要定义它

public class Shoes
{

    private List<Shoe> shoes;


    public List<Shoe> getShoes()
    {
        return shoes;
    }

    public void setShoes(List<Shoe> shoes)
    {
        this.shoes = shoes;
    }
}

Assume jsonData is the JSON input as String, below code would convert JSON string to "Shoes" object which contain List 假设jsonData是作为String的JSON输入,下面的代码将JSON字符串转换为包含List的“Shoes”对象

Gson gson = new Gson();

Type dataType = (new TypeToken<Shoes>()
{
}).getType();

Shoes shoeList = gson.fromJson(jsonData, dataType);

for(Shoe e: shoeList.getShoes()) {
    System.out.println(e);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM