LitJSON / litjson

JSON library for the .Net framework
https://litjson.net/
Other
1.36k stars 403 forks source link

[using in unity] can't assign ‘xxx’ (type System.String) to type ListJson.JsonData #117

Open caochao opened 4 years ago

caochao commented 4 years ago

it's quite a strange problem when I build my game to iOS platform and using JsonMapper.ToObject<List<JsonData>>("some json string") to parse object array json string. json string is "[{"BattleCfg":"10_03_001","SceneName":"RoomBall","Item1":"1","Prev":"0","Item2":"2","Next":"2","Hp":"8","Item3":"3","Id":"1","Name":"01"}]". however, this codes works fine in unity editor...

devlead commented 4 years ago

Hi,

Which version of LitJSON are you using? 0.16.0? https://www.nuget.org/packages/LitJson/0.16.0

What type is List?

Did a quick .NET Fiddle with above json just using generic types and 0.16.0, can you repro there? https://dotnetfiddle.net/hEPaE1

Executing this code

// Serialize JSON to object
var fromJson = "[{\"BattleCfg\":\"10_03_001\",\"SceneName\":\"RoomBall\",\"Item1\":\"1\",\"Prev\":\"0\",\"Item2\":\"2\",\"Next\":\"2\",\"Hp\":\"8\",\"Item3\":\"3\",\"Id\":\"1\",\"Name\":\"01\"}]";
var fromObject = LitJson.JsonMapper.ToObject<List<Dictionary<string, string>>>(fromJson);   
foreach(var dictionary in fromObject)
{
    foreach(var kv in dictionary)
    {
        Console.WriteLine(kv);
    }
}

outputs

[BattleCfg, 10_03_001]
[SceneName, RoomBall]
[Item1, 1]
[Prev, 0]
[Item2, 2]
[Next, 2]
[Hp, 8]
[Item3, 3]
[Id, 1]
[Name, 01]

So it looks like it at least can parse example json.

Also did a test .NET Fiddle using a defined class https://dotnetfiddle.net/skCkMG

    public class Config
    {
        public string BattleCfg { get; set; }
        public string SceneName { get; set; }
        public string Item1 { get; set; }
        public string Prev { get; set; }
        public string Item2 { get; set; }
        public string Next { get; set; }
        public string Hp { get; set; }
        public string Item3 { get; set; }
        public string Id { get; set; }
        public string Name { get; set; }

        public override string ToString()
        {
            return string.Format(
                "BattleCfg: {0}, SceneName: {1}, Item1: {2}, Prev: {3}, Item2: {4}, Next: {5}, Hp: {6}, Item3; {7}, Id: {8}, Name: {9}",
                BattleCfg,
                SceneName,
                Item1,
                Prev,
                Item2,
                Next,
                Hp,
                Item3,
                Id,
                Name
            );
        }
    }

and code

// Serialize JSON to object
var fromJson = "[{\"BattleCfg\":\"10_03_001\",\"SceneName\":\"RoomBall\",\"Item1\":\"1\",\"Prev\":\"0\",\"Item2\":\"2\",\"Next\":\"2\",\"Hp\":\"8\",\"Item3\":\"3\",\"Id\":\"1\",\"Name\":\"01\"}]";
var fromObject = LitJson.JsonMapper.ToObject<Config[]>(fromJson);   
foreach(var config in fromObject)
{
    Console.WriteLine(config);
}

outputted

BattleCfg: 10_03_001, SceneName: RoomBall, Item1: 1, Prev: 0, Item2: 2, Next: 2, Hp: 8, Item3; 3, Id: 1, Name: 01

So unfortunately not able to reproduce your error.

caochao commented 4 years ago

sorry for my unclear description. I'm using litjson 0.16.0 with JsonMapper.ToObject<List<JsonData>>("some json string") method. actually it works well in unity. Exceptions occurred in iOS platform, litjson can't find implicit convertion from String to JsonData, maybe it's caused by reflection using in iOS platform?