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

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

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

  1. [{"county":"Jefferson",
  2. "houses":\[
  3. {"squareFeet":1100,
  4. "bedrooms":2,
  5. "bathrooms":2,
  6. "internet":"y",
  7. "location":"Country"
  8. },
  9. {"squareFeet":750,
  10. "bedrooms":1,
  11. "bathrooms":1,
  12. "internet":"n",
  13. "location":"Town"
  14. }
  15. \]
  16. }]

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

  1. import com.google.gson.Gson;
  2. import com.google.gson.GsonBuilder;
  3. import org.json.simple.JSONArray;
  4. import org.json.simple.JSONObject;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.StringWriter;
  8. import java.util.ArrayList;
  9. import java.util.Formatter;
  10. import java.util.List;
  11. public class HousesToJSON {
  12. /**
  13. * @param args
  14. * @throws IOException
  15. */
  16. public static void main(String[] args) throws IOException {
  17. Gson gson = new GsonBuilder().setPrettyPrinting().create();
  18. JSONArray houses = new JSONArray();
  19. House houseOne = createHouseObjectOne();
  20. House houseTwo = createHouseObjectTwo();
  21. houses.add(houseOne);
  22. houses.add(houseTwo);
  23. try (FileWriter writer = new FileWriter("houses.json")) {
  24. gson.toJson(houses, writer);
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. private static House createHouseObjectOne() {
  30. House house = new House();
  31. house.setSquareFeet(1100);
  32. house.setBedrooms(2);
  33. house.setBathrooms(2);
  34. house.setInternet('y');
  35. house.setLocation("Country");
  36. return house;
  37. }
  38. private static House createHouseObjectTwo() {
  39. House house = new House();
  40. house.setSquareFeet(750);
  41. house.setBedrooms(2);
  42. house.setBathrooms(1);
  43. house.setInternet('y');
  44. house.setLocation("Town");
  45. return house;
  46. }
  47. }

这将创建下面的文件。

  1. [
  2. {
  3. "squareFeet": 1100,
  4. "bedrooms": 2,
  5. "bathrooms": 2,
  6. "internet": "y",
  7. "location": "Country"
  8. },
  9. {
  10. "squareFeet": 750,
  11. "bedrooms": 2,
  12. "bathrooms": 1,
  13. "internet": "y",
  14. "location": "Town"
  15. }
  16. ]

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

xn1cxnb4

xn1cxnb41#

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

  1. package com.example;
  2. import java.util.List;
  3. import com.google.gson.annotations.Expose;
  4. import com.google.gson.annotations.SerializedName;
  5. public class CountyContainer {
  6. @SerializedName("county")
  7. @Expose
  8. private String county;
  9. @SerializedName("houses")
  10. @Expose
  11. private List<House> houses = null;
  12. public String getCounty() {
  13. return county;
  14. }
  15. public void setCounty(String county) {
  16. this.county = county;
  17. }
  18. public List<House> getHouses() {
  19. return houses;
  20. }
  21. public void setHouses(List<House> houses) {
  22. this.houses = houses;
  23. }
  24. }
  1. package com.example;
  2. import com.google.gson.annotations.Expose;
  3. import com.google.gson.annotations.SerializedName;
  4. public class House {
  5. @SerializedName("squareFeet")
  6. @Expose
  7. private Integer squareFeet;
  8. @SerializedName("bedrooms")
  9. @Expose
  10. private Integer bedrooms;
  11. @SerializedName("bathrooms")
  12. @Expose
  13. private Integer bathrooms;
  14. @SerializedName("internet")
  15. @Expose
  16. private String internet;
  17. @SerializedName("location")
  18. @Expose
  19. private String location;
  20. public Integer getSquareFeet() {
  21. return squareFeet;
  22. }
  23. public void setSquareFeet(Integer squareFeet) {
  24. this.squareFeet = squareFeet;
  25. }
  26. public Integer getBedrooms() {
  27. return bedrooms;
  28. }
  29. public void setBedrooms(Integer bedrooms) {
  30. this.bedrooms = bedrooms;
  31. }
  32. public Integer getBathrooms() {
  33. return bathrooms;
  34. }
  35. public void setBathrooms(Integer bathrooms) {
  36. this.bathrooms = bathrooms;
  37. }
  38. public String getInternet() {
  39. return internet;
  40. }
  41. public void setInternet(String internet) {
  42. this.internet = internet;
  43. }
  44. public String getLocation() {
  45. return location;
  46. }
  47. public void setLocation(String location) {
  48. this.location = location;
  49. }
  50. }

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

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

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

  1. 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 提供课程,但我想我们可以跳过。你正朝着正确的方向前进。只需添加另一个类:

  1. public class County {
  2. private String county;
  3. private List<House> houses;
  4. // getters, setters, whatever :)
  5. }

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

  1. County county = new County();
  2. county.setCounty("Jefferson");
  3. List<House> houses = new ArrayList<>();
  4. houses.add(houseOne);
  5. houses.add(houseTwo);
  6. county.setHouses(houses);
  7. counties.add(county);

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

展开查看全部

相关问题