首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Json映射适用于键,但不适用于值

Json映射适用于键,但不适用于值
EN

Stack Overflow用户
提问于 2021-01-23 06:30:42
回答 1查看 23关注 0票数 1

我的json映射的格式如下所示,它适用于键,但不适用于值(没有显示任何内容,有时甚至是空的),如果你有一个想法,它将对我有很大帮助。

代码语言:javascript
复制
{"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]}]}
代码语言:javascript
复制
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);
            }
        }
    }
}

EN

回答 1

Stack Overflow用户

发布于 2021-01-24 11:09:10

正如Aluan Haddad指出的那样,使用pos而不是position

代码语言:javascript
复制
public class Agent
{
  public bool isAgent { get; set; }
  public bool hasHV { get; set; }
  public int[] pos { get; set; }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65853595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档