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);
}
}
}
}
question from:
https://stackoverflow.com/questions/65853595/json-mapping-works-for-keys-but-not-values