简体   繁体   English

将包含另一个 JSON 字符串的 JSON 对象解析为对象

[英]Parsing JSON object that contains another JSON String to a Object

Currently I am working on a school project where I had to build a Game in which a user could customize a deck of Cards and save this to a Database via a REST API.目前我正在从事一个学校项目,我必须构建一个游戏,用户可以在其中自定义一副纸牌并通过 REST API 将其保存到数据库中。 This part I got successfully working, however I decided to save the deck of Cards as a JSON String into my database table.这部分我成功地工作了,但是我决定将一副卡片作为 JSON 字符串保存到我的数据库表中。 Now I am having issues whilst using Gson to parse the object back to the correct Model as it is expecting a properly typed list as opposed to a Json String.现在我在使用 Gson 将对象解析回正确的模型时遇到问题,因为它期望正确键入的列表而不是 Json 字符串。

Does anyone have a good solution to easily parse this object or to extract the deck of cards from the Json String before parsing the rest of the object?有没有人有一个很好的解决方案来轻松解析这个对象或在解析对象的其余部分之前从 Json 字符串中提取一副纸牌?

I will provide some example code to show the current structure of my object and Json:我将提供一些示例代码来显示我的对象和 Json 的当前结构:

Model to Convert to要转换为的模型

public class CharacterModel{
private String characterName;
private int maxHp
private List<BattleCard> cardDeck

public CharacterModel(){}

//Getters and Setters for all paramters below
}

Json Format Json 格式

{
  "characterName": "TestCharacter;",
  "maxHp": "4",
  "cardDeck": "{Json String with the List of Cards here}"
}

What would be the best way to resolve this?解决这个问题的最佳方法是什么? As due to the way the JSON String gets nested into the parent JSON String it is not recognized as a object by Gson when trying to convert.由于 JSON 字符串嵌套到父 JSON 字符串中的方式,尝试转换时 Gson 无法将其识别为对象。 Any help with this problem would be very much appreciated.任何有关此问题的帮助将不胜感激。

Generally, nowadays it's more likely to use JSON-B and/or JSON-P since it's part of the java specification.通常,现在更可能使用JSON-B和/或JSON-P,因为它是 Java 规范的一部分。 However, GSON supports binding too.但是,GSON 也支持绑定。 When you're working with POJOs / Entities, you usually don't have to generate or parse any JSON by hand in 2020. Binding is the word you're looking for ;)当您使用 POJO/实体时,您通常不必在 2020 年手动生成或解析任何 JSON。绑定是您正在寻找的词;)

I've implemented an example for this with JSON-B and GSON, they don't differ much anyway in this case.我已经使用 JSON-B 和 GSON 为此实现了一个示例,无论如何在这种情况下它们没有太大区别。

    Card jsonbCard = new Card();
    jsonbCard.name = "JSON-B";
    jsonbCard.value = 9001;
    Card gsonCard = new Card();
    gsonCard.name = "GSON";
    gsonCard.value = -9001;
    
    List<Card> cardList = new ArrayList<>();
    cardList.add(jsonbCard);
    cardList.add(gsonCard);
    
    Jsonb jsonb = JsonbBuilder.create();
    String listSerialized = jsonb.toJson(cardList);
    System.out.println(listSerialized);
    
    List<Card> cardListFromJson = jsonb.fromJson(listSerialized, new ArrayList<Card>(){}.getClass().getGenericSuperclass());

    Gson gson = new Gson();  
    List<Card> cardListFromJson_GSON = gson.fromJson(listSerialized,  new TypeToken<List<Card>>(){}.getType());

This generated the following JSON string: [{"name":"JSON-B","value":9001},{"name":"GSON","value":-9001}] and deserializes the string back into a List of Card .这生成了以下 JSON 字符串: [{"name":"JSON-B","value":9001},{"name":"GSON","value":-9001}]并将字符串反序列化回Card List

So in your case, you can所以在你的情况下,你可以

  1. create another class containing cardDeck as a String, bind the JSON to that, create the actual Character class and set the List by binding that cardDeck String to a List .创建另一个包含cardDeck作为 String 的类,将 JSON 绑定到该类,创建实际的Character类并通过将该 cardDeck String绑定到List来设置List
  2. use parsing and extract the cardDeck String from the JSON, remove it from the JSON, parse it to the object, parse the extracted cardDeck String to a List and set it on the previously arsed object.使用解析并从 JSON 中提取cardDeck字符串,将其从 JSON 中删除,将其解析为对象,将提取的cardDeck字符串解析为列表并将其设置在先前的对象上。
  3. serialize properly, and you don't have this mess ;)正确序列化,你就没有这个烂摊子;)

The main mistake is that the cardDeck in your JSON shouldn't be a String to begin with.主要错误是 JSON 中的cardDeck不应该是String开头。

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

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