我如何获得信息从一个对象与json中的对象数组为Unity?



我有一个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
}
]
}

我也有一个Cards脚本

[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;
}

然后我有一个cardeeffects脚本,我正在使用我的函数。

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.");
}
}
}

我到处搜索,试图弄清楚如何获得"值"的对象数组。到目前为止,输出的值总是0不管values&quot中的信息是什么在JSON中。如果我使类Serializable,我能够改变的值,它的工作,但我希望值是他们在JSON中声明的任何东西。有更好的方法吗?

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

您的jsonclasses不匹配。不仅你的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;
}

我建议使用MiniJson。您可以将脚本复制粘贴到您的项目中,然后就可以开始了。然后可以调用Json.Deserialize(string json)传递你的字符串。对于数组,您可以这样做:

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

查找工作代码段:

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();
}
}
}