所以我尝试使用gson创建这个json文件

bcs8qyzn  于 2021-06-27  发布在  Java
关注(0)|答案(2)|浏览(416)

下面是我想用java代码创建的代码。我没有做任何详细的事情。只是想用解析java中的json来刷新自己。

[{"county":"Jefferson",
    "houses":\[
        {"squareFeet":1100,
        "bedrooms":2,
        "bathrooms":2,
        "internet":"y",
        "location":"Country"
        },
        {"squareFeet":750,
        "bedrooms":1,
        "bathrooms":1,
        "internet":"n",
        "location":"Town"
        }
    \]
}]

目前我的java代码是这样的。
有了下面的代码,除了第一个对象,以及房屋阵列的所有权,我就快拥有它了。

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;

public class HousesToJSON {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        Gson gson = new GsonBuilder().setPrettyPrinting().create();

        JSONArray houses = new JSONArray();

        House houseOne = createHouseObjectOne();
        House houseTwo = createHouseObjectTwo();

        houses.add(houseOne);
        houses.add(houseTwo);

        try (FileWriter writer = new FileWriter("houses.json")) {
            gson.toJson(houses, writer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static House createHouseObjectOne() {
        House house = new House();

        house.setSquareFeet(1100);
        house.setBedrooms(2);
        house.setBathrooms(2);
        house.setInternet('y');
        house.setLocation("Country");

        return house;
    }

    private static House createHouseObjectTwo() {
        House house = new House();

        house.setSquareFeet(750);
        house.setBedrooms(2);
        house.setBathrooms(1);
        house.setInternet('y');
        house.setLocation("Town");

        return house;
    }
}

这将创建下面的文件。

[
  {
    "squareFeet": 1100,
    "bedrooms": 2,
    "bathrooms": 2,
    "internet": "y",
    "location": "Country"
  },
  {
    "squareFeet": 750,
    "bedrooms": 2,
    "bathrooms": 1,
    "internet": "y",
    "location": "Town"
  }
]

我在这方面还很新,任何帮助都将不胜感激。

xn1cxnb4

xn1cxnb41#

与此类似的内容应该为您的json示例提供必要的对象:

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class CountyContainer {

 @SerializedName("county")
 @Expose
 private String county;
 @SerializedName("houses")
 @Expose
 private List<House> houses = null;

 public String getCounty() {
  return county;
 }

 public void setCounty(String county) {
  this.county = county;
 }

 public List<House> getHouses() {
  return houses;
 }

 public void setHouses(List<House> houses) {
  this.houses = houses;
 }
}
package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class House {

 @SerializedName("squareFeet")
 @Expose
 private Integer squareFeet;
 @SerializedName("bedrooms")
 @Expose
 private Integer bedrooms;
 @SerializedName("bathrooms")
 @Expose
 private Integer bathrooms;
 @SerializedName("internet")
 @Expose
 private String internet;
 @SerializedName("location")
 @Expose
 private String location;

 public Integer getSquareFeet() {
  return squareFeet;
 }

 public void setSquareFeet(Integer squareFeet) {
  this.squareFeet = squareFeet;
 }

 public Integer getBedrooms() {
  return bedrooms;
 }

 public void setBedrooms(Integer bedrooms) {
  this.bedrooms = bedrooms;
 }

 public Integer getBathrooms() {
  return bathrooms;
 }

 public void setBathrooms(Integer bathrooms) {
  this.bathrooms = bathrooms;
 }

 public String getInternet() {
  return internet;
 }

 public void setInternet(String internet) {
  this.internet = internet;
 }

 public String getLocation() {
  return location;
 }

 public void setLocation(String location) {
  this.location = location;
 }
}

您可以使用此在线服务并选择gson注解样式自行生成这些注解:
http://www.jsonschema2pojo.org/
创建对象后,可以使用以下命令将这些对象写入json文件:

final Gson gson = new Gson();
try (final FileWriter writer = new FileWriter("houses.json")) {
 gson.toJson(houses, writer); // houses refers to your object containing the List of House objects and the country
} catch (final IOException e) {
 // Handle exceptions here
}

或者,您也可以使用将json数据放入 String :

String json = gson.toJson(houses);

有关gson功能的更多信息,请查看官方文档:
https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/module-summary.html

a11xaf1n

a11xaf1n2#

根本没有 House 提供课程,但我想我们可以跳过。你正朝着正确的方向前进。只需添加另一个类:

public class County {
    private String county;
    private List<House> houses;

    // getters, setters, whatever :) 
}

并创建该类的示例。设置name(county字段)和houses并序列化为json:)
我可能会将json数组重命名为 JSONArray counties = new JSONArray(); 您需要添加到这个数组中的是一个名为“jefferson”的县和您现在拥有的房屋列表。

County county = new County();
county.setCounty("Jefferson");

List<House> houses = new ArrayList<>();
houses.add(houseOne);
houses.add(houseTwo);

county.setHouses(houses);

counties.add(county);

这可能不是100%工作的代码,但希望想法很清楚:)

相关问题