Unity查看JSON文本 使用Unity自己提供了一个JSON data的序列化与反序列化的类JsonUtility

json数据文本                                                                                                 

{
    "lsPlayerMessage": [
        {
            "PlayerId": 1,
            "PlayerName": "Json",
            "PlayerEquip": [
                {
                    "EquipId": 101,
                    "EquipName": "Gun"
                }
            ]
        },
        {
            "PlayerId": 2,
            "PlayerName": "Json",
            "PlayerEquip": [
                {
                    "EquipId": 102,
                    "EquipName": "Gun_1"
                }
            ]
        }
    ]
}

Unity中实现json数据遍历代码如下

    void ReadJson()
    {
        // JsonPath()自己写的json文件所在的目录
        if (!File.Exists(JsonPath()))
        {
            Debug.LogError("读取的文件不存在!");
            return;
        }
        string json = File.ReadAllText(JsonPath());
        JsonData jsonTemp = new JsonData();
        jsonTemp = JsonUtility.FromJson<JsonData>(json);
        Debug.Log("玩家数量:" + jsonTemp.lsPlayerMessage.Count);
        for (int i = 0; i < jsonTemp.lsPlayerMessage.Count; i++)
        {
            Debug.Log(jsonTemp.lsPlayerMessage[i].PlayerId);
            Debug.Log(jsonTemp.lsPlayerMessage[i].PlayerName);
            for (int j = 0; j < jsonTemp.lsPlayerMessage[i].PlayerEquip.Count; j++)
            {
                Debug.Log(jsonTemp.lsPlayerMessage[i].PlayerEquip[j].EquipId);
                Debug.Log(jsonTemp.lsPlayerMessage[i].PlayerEquip[j].EquipName);
            }
        }
    }
}


public class JsonData
{
    public List<PlayerMessage> lsPlayerMessage;
}

// 玩家信息
// 序列化
[Serializable]
public class PlayerMessage
{
    public int PlayerId;
    public string PlayerName;
    public List<EquipMessage> PlayerEquip;
}

// 装备信息
// 序列化
[Serializable]
public class EquipMessage
{
    public int EquipId;
    public string EquipName;
}

 现在举个例子给你,自己照着写出来看看(温馨提示....json这里取出来不是二维数组)答案(=c=我的答案)在最下面

{
    "level" : [
        {
            "levelcontent": [
                {
                    "name" : 1,
                    "content" : [
                        0,0,1,1,1,0,0,0,
                        0,0,1,3,1,0,0,0,
                        0,0,1,2,1,1,1,1,
                        1,1,1,4,2,4,3,1,
                        1,3,2,4,2,1,1,1,
                        1,1,1,1,4,1,0,0,
                        0,0,0,1,3,1,0,0,
                        0,0,0,1,1,1,0,0
                    ],
                    "allRow" : 8,  
                    "allCol" : 8,
                    "heroRow" : 4,
                    "heroCol" : 4,
                    "allBox" : 4
                }
            ]
        }
    ]
}

 

 

 

 

 

    // Application.streamingAssetsPath 
    // 指向 X:/unity_file/项目文件名/Assets/StreamingAssets 这个文件夹
    string mapFile = Application.streamingAssetsPath + "/levelConfig.json";

        // print(mapFile);

        if (!File.Exists(mapFile))
        {
            Debug.LogError("读取的文件不存在!");
            return;
        }
        string json = File.ReadAllText(mapFile);

        Debug.Log("json数据:" + json);

        JsonData jsonTemp = new JsonData();
        jsonTemp = JsonUtility.FromJson<JsonData>(json);
        Debug.Log("关卡数量:" + jsonTemp.level.Count);
        for (int i = 0; i < jsonTemp.level.Count; i++)
        {
            for (int j = 0; j < jsonTemp.level[i].levelcontent.Count; j++)
            {
                Debug.Log(jsonTemp.level[i].levelcontent[j].name);
                Debug.Log(jsonTemp.level[i].levelcontent[j].content);
            }
        }


    }

    // 预制一个json数据类
    [Serializable]
    public class JsonData
    {
        public List<Level> level;
    }
    // 关卡
    [Serializable]
    public class Level
    {
        public List<Levelcontent> levelcontent;
    }
    // 关卡信息
    [Serializable]
    public class Levelcontent
    {
        public int[] content;
        public int name, allRow, allCol, heroRow, heroCol, allBox;
    }

如果比较迷糊 可以看看https://www.jianshu.com/p/b561719da1fe大佬的文字

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用如下代码实现: ``` using Newtonsoft.Json; using System.Collections.Generic; public class MyDictionaryConverter : JsonConverter { public override bool CanConvert(Type objectType) { return objectType.IsGenericType && objectType.GetGenericTypeDefinition() == typeof(Dictionary<,>); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { var dictionary = (IDictionary)value; var keyType = dictionary.GetType().GetGenericArguments()[0]; var valueType = dictionary.GetType().GetGenericArguments()[1]; writer.WriteStartObject(); foreach (var key in dictionary.Keys) { writer.WritePropertyName(JsonConvert.SerializeObject(key)); serializer.Serialize(writer, dictionary[key]); } writer.WriteEndObject(); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var dictionary = (IDictionary)Activator.CreateInstance(objectType); var keyType = objectType.GetGenericArguments()[0]; var valueType = objectType.GetGenericArguments()[1]; while (reader.Read() && reader.TokenType == JsonToken.PropertyName) { var propertyName = JsonConvert.DeserializeObject(reader.Value.ToString(), keyType); var value = serializer.Deserialize(reader, valueType); dictionary.Add(propertyName, value); } return dictionary; } } ``` 使用方法如下: ``` // 序列 Dictionary<string, object> dic = new Dictionary<string, object>(); string json = JsonConvert.SerializeObject(dic, new MyDictionaryConverter()); // 序列 Dictionary<string, object> result = JsonConvert.DeserializeObject<Dictionary<string, object>>(json, new MyDictionaryConverter()); ``` 需要注意的是,需要在属性上添加 `[JsonConverter(typeof(MyDictionaryConverter))]` 特性来告诉 `Newtonsoft.Json` 使用上述自定义的 `MyDictionaryConverter` 来进行序列序列

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值