Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
79 views
in Technique[技术] by (71.8m points)

c# - Json mapping works for keys but not values

my json mapping whose format is given below works for the keys but not for the values (nothing is displayed or sometimes NULL), if you have an idea it would help me a lot.

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

enter image description here

question from:https://stackoverflow.com/questions/65853595/json-mapping-works-for-keys-but-not-values

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As Aluan Haddad had pointed out Use pos instead of position

public class Agent
{
  public bool isAgent { get; set; }
  public bool hasHV { get; set; }
  public int[] pos { get; set; }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...