【问题标题】:How do I get info from an object with an array of objects in a json for Unity?如何从 Unity 的 json 中包含对象数组的对象获取信息?
【发布时间】:2021-10-25 19:31:06
【问题描述】:

我有一个这样的 JSON 文件设置

{
  "cards": [
    {
      "id": "sand_bags",
      "type": "Structure",
      "name": "Sand Bags",
      "values": [
        {
          "civilian": 1,
          "fiend": -1
        }
      ],
      "effectTexts": [
        {
          "civilian": "Add +1 to your BUNKER.",
          "fiend": "Send any CIVILIAN'S +1 STRUCTURE to the SCRAPYARD."
        }
      ],
      "flavorTexts": [
        {
          "civilian": "They're just heavy bags with sand. Not much else to say, but they'll slow down an attack from a fiend. Good luck, you'll need it!",
          "fiend": "You've spotted a pretty bad STRUCTURE in this BUNKER. Time to do some damage."
        }
      ],
      "staysOnField": [
        {
          "civilian": true,
          "fiend": false
        }
      ],
      "amountInDeck": 5
    }
  ]
}

我也有一个卡片脚本

[Serializable]
public class Cards
{
    public Card[] cards;
}

[Serializable]
public class Card
{
    public string id;
    public string type;
    public string name;

    public int amountInDeck;
}

public class Values
{
    public int civilian;
    public int fiend;
}

然后我有一个 CardEffects 脚本用于我的功能。

public class CardEffects : MonoBehaviour
{
    public TextAsset jsonFile;

    public Values values;

    void Start()
    {
        Cards cardsInJson = JsonUtility.FromJson<Cards>(jsonFile.text);

        foreach (Card card in cardsInJson.cards)
        {
            Debug.Log("Card name: " + card.name + " with " + values.civilian + " in the deck.");
        }
    }
}

我已经搜索了所有内容,试图找出如何获取“值”对象的数组。我做到了这一点,无论 JSON 中“值”中的信息如何,打印的值始终为 0。如果我使类可序列化,我可以更改值并且它可以工作,但我希望这些值是它们在 JSON 中声明的任何值。有没有更好的方法来做到这一点?

请记住,我是 C# 和 Unity 的新手。我通常用 JS 编写代码,其中使用 JSON 文件对我来说没什么大不了的,我认为这是最好的方法。

【问题讨论】:

  • 我认为您的班级Card 中根本没有字段Card ...所以添加例如public Values[] values;,你应该没问题......一般来说,在 Unity 上下文中,你可能还想稍后添加项目,我总是更喜欢列表而不是数组;)

标签: c# json unity3d


【解决方案1】:

您的 json 和您的 classes 不匹配。不仅您的json 甚至不是有效的Json。下面我给你正确的jsonclass

{
  "cards": [
    {
      "id": "sand_bags",
      "type": "Structure",
      "name": "Sand Bags",
      "values": [
        {
          "civilian": 1,
          "fiend": -1
        }
      ],
      "effectTexts": [
        {
          "civilian": "Add +1 to your BUNKER.",
          "fiend": "Send any CIVILIAN'S +1 STRUCTURE to the SCRAPYARD."
        }
      ],
      "flavorTexts": [
        {
          "civilian": "They're just heavy bags with sand. Not much else to say, but they'll slow down an attack from a fiend. Good luck, you'll need it!",
          "fiend": "You've spotted a pretty bad STRUCTURE in this BUNKER. Time to do some damage."
        }
      ],
      "staysOnField": [
        {
          "civilian": true,
          "fiend": false
        }
      ],
      "amountInDeck": 5
    }
  ] // <---- This was missing
}

对于那个 Json,这就是你的 card 类的样子:

public Card 
{
    public string id { get; set; }
    public string type { get; set; }
    public string name { get; set; }
    public Values[] values { get; set; }
    public Values[] effectTexts { get; set; }
    public Values[] staysOnField { get; set; }
    public int amountInDeck { get; set; }
}

你的Values 类必须是这样的:

public class Values
{
    public object civilian;
    public object fiend;
}

【讨论】:

  • JSON 设置正确。我只从中取了一个元素,忘记在我原来的帖子中关闭它。 {得到;放; } 至少对于 InDeck 的 id、类型、名称和数量来说是不需要的。我能够使用所有这些并获得正确的信息。但是,我将尝试在有和没有 get set 的情况下声明数组
  • 我之前更改了代码并犯了一个快速错误,所以这是一个快速编辑。当我将 CardEffects 中的声明从 public Values 更改为 public Values[] 并在 Debug.Log 中执行 values[0].civilian 时,我得到“索引超出了数组的范围”。如果我将其保留为 public Values 并将调试更改为 values.civilian,则一切仍将返回为 0。
【解决方案2】:

我建议MiniJson。 您只需将脚本复制粘贴到您的项目中,就可以开始了。 然后可以调用Json.Deserialize(string json) 传递您的字符串。对于数组的情况,您可以执行以下操作:

IList myJsonElements = (IList)Json.Deserialize(string json);

找到工作的sn-p:

using System;
using Newtonsoft.Json;

namespace ConsoleApp3
{
  class Program
  {
    static void Main(string[] args) {
      string myJson = "{\"cards\": [{\"id\": \"sand_bags\",\"type\": \"Structure\",\"name\": \"Sand Bags\",\"values\": [{\"civilian\": 1,\"fiend\": -1}],\"effectTexts\": [{\"civilian\": \"Add +1 to your BUNKER.\",\"fiend\": \"Send any CIVILIAN'S +1 STRUCTURE to the SCRAPYARD.\"}],\"flavorTexts\": [{\"civilian\": \"They're just heavy bags with sand. Not much else to say, but they'll slow down an attack from a fiend. Good luck, you'll need it!\",\"fiend\":\"You've spotted a pretty bad STRUCTURE in this BUNKER. Time to do some damage.\"}],\"staysOnField\": [{\"civilian\": true,\"fiend\": false}],\"amountInDeck\": 5}]}";
      var myJsonElements = MiniJSON.Json.Deserialize(myJson);
      Console.WriteLine(myJsonElements.ToString());
      string json = JsonConvert.SerializeObject(myJsonElements, Formatting.Indented);
      Console.WriteLine(json);
      Console.ReadLine();
    }
  }
}

【讨论】:

  • 这并不能真正回答我的问题。只是增加了更多的问题,让事情对我来说更复杂。我确实在这段代码的最后才能让它工作,要么需要帮助调整以使其工作,要么需要另一种与 JSON 无关的方法来更好地工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多