我的json映射的格式如下所示,它适用于键,但不适用于值(没有显示任何内容,有时甚至是空的),如果你有一个想法,它将对我有很大帮助。
{"0": [{"agents": true, "hv": true, "pos": [8, 7]}, {"agents": true, "hv": true, "pos": [1, 9]}, {"agents": true, "hv": true, "pos": [6, 5]}, {"agents": true, "hv": true, "pos": [4, 1]}, {"agents": true, "hv": true, "pos": [1, 4]}, {"agents": true, "hv": true, "pos": [2, 2]}, {"agents": false, "hv": false, "pos": [1, 6]}, {"agents": false, "hv": false, "pos": [5, 2]}, {"agents": false, "hv": false, "pos": [9, 5]}, {"agents": false, "hv": false, "pos": [8, 5]}, {"agents": false, "hv": true, "pos": [3, 6]}, {"agents": false, "hv": true, "pos": [8, 9]}], "1": [{"agents": true, "hv": true, "pos": [7, 7]}, {"agents": true, "hv": true, "pos": [1, 8]}, {"agents": true, "hv": true, "pos": [6, 5]}, {"agents": true, "hv": true, "pos": [4, 1]}, {"agents": true, "hv": true, "pos": [1, 4]}, {"agents": true, "hv": true, "pos": [2, 1]}, {"agents": false, "hv": false, "pos": [1, 6]}, {"agents": false, "hv": false, "pos": [5, 2]}, {"agents": false, "hv": false, "pos": [9, 5]}, {"agents": false, "hv": false, "pos": [8, 5]}, {"agents": false, "hv": true, "pos": [4, 6]}, {"agents": false, "hv": true, "pos": [9, 9]}]}public class Agent
{
public bool isAgent { get; set; }
public bool hasHV { get; set; }
public int[] position { get; set; }
}`
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JSONReader : MonoBehaviour
{
public TextAsset textJson;
Dictionary<string, Agent[]> agentMap = new Dictionary<string, Agent[]>();
private void Start()
{
agentMap = JsonConvert.DeserializeObject<Dictionary<string, Agent[]>>(textJson.text);
print(agentMap);
foreach (KeyValuePair<string, Agent[]> entry in agentMap)
{
print("Key = "+entry.Key);
Agent[] agents = entry.Value;
foreach (Agent agent in agents)
{
print("Value = "+agent.position);
}
}
}
}

发布于 2021-01-24 11:09:10
正如Aluan Haddad指出的那样,使用pos而不是position
public class Agent
{
public bool isAgent { get; set; }
public bool hasHV { get; set; }
public int[] pos { get; set; }
}https://stackoverflow.com/questions/65853595
复制相似问题