Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
caochao opened this issue May 26, 2020 · 2 comments

Comments

@caochao
Copy link

caochao commented May 26, 2020

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
Copy link
Member

devlead commented May 27, 2020

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
Copy link
Author

caochao commented May 28, 2020

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants