我有一個 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。如果我將類設為 Serializable,我就可以更改這些值并且它可以作業,但我希望這些值是它們在 JSON 中宣告的任何內容。有一個更好的方法嗎?
請記住,我是 C# 和 Unity 的新手。我通常用 JS 撰寫代碼,其中使用 JSON 檔案對我來說沒什么大不了的,并認為這是最好的方法。
uj5u.com熱心網友回復:
你的json和你的classes不匹配。不僅如此,您json甚至不是有效的 Json。下面我將給你正確的json和class。
{
"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;
}
uj5u.com熱心網友回復:
我建議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();
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/336856.html
