FastJson 反序列化Dictionary

sakura03jc 2015-07-04 07:05:08
需要反序列化的Json为 {"test1":0,"test2":"This is a test"} ,这样既有int,也有string类型的,所以需要Dictionary<string, object>类型,但在使用FastJson反序列化时,调用
Dictionary<string, object> d = FastJson.Deserialize<Dictionary<string, object>>(json);
会报错 FormatException: failed read char '{'. index = 10 text = "t1":0,"tes"
想要的结果是
d["test1"] = (int)0;
d["test2"] = (string)"This is a test";
请问怎样解决呢?感谢!!
...全文
338 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
sakura03jc 2015-07-05
  • 打赏
  • 举报
回复
引用 6 楼 xuzuning 的回复:
改为 {"test1":"0"} 应该也不会出错 string 算对象(object) int 就不算对象了吧?
也是不行的,必须明确指明是string才可以,我也觉得很奇怪
xuzuning 2015-07-05
  • 打赏
  • 举报
回复
改为 {"test1":"0"} 应该也不会出错 string 算对象(object) int 就不算对象了吧?
sakura03jc 2015-07-05
  • 打赏
  • 举报
回复
引用 1 楼 shingoscar 的回复:
你确定是反序列化时候出的错吗? FormatException一般是string.Format的第一个参数传错了
我确定参数没错,因为我后来测试把Json改为 {"test1":0} 然后把object改为int Dictionary<string, int> d = FastJson.Deserialize<Dictionary<string, int>>(json); 这样就不报错了
sakura03jc 2015-07-05
  • 打赏
  • 举报
回复
引用 3 楼 sp1234 的回复:
另外从你的错误信息来看,可能是双引号错了。
应该不是双引号的问题,我尝试把test2删掉,仅留test1,再把Dictionary改为<string,int>类型,就正常了。
sakura03jc 2015-07-05
  • 打赏
  • 举报
回复
谢谢各位的帮助,结贴了
sakura03jc 2015-07-05
  • 打赏
  • 举报
回复
引用 9 楼 shingoscar 的回复:
不知道你的FastJson是从哪来的,我用nuget下载的fastjson2.1.14.0,没有你使用的方式
是公司花钱买的最新版。。刚才我从网上下载了和你一样的这个版本,然后就可以用了,好晕啊
Poopaye 2015-07-05
  • 打赏
  • 举报
回复
不知道你的FastJson是从哪来的,我用nuget下载的fastjson2.1.14.0,没有你使用的方式
namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			string k = "{\"test1\":0,\"test2\":\"This is a test\"}";
			Dictionary<string, object> d = fastJSON.JSON.ToObject<Dictionary<string, object>>(k);

			foreach (var item in d)
			{
				Console.WriteLine(item.Key + "->" + item.Value);
			}
		}
	}
}
xuzuning 2015-07-05
  • 打赏
  • 举报
回复
那就是说,至少在这里 string 也不算 object
  • 打赏
  • 举报
回复
另外从你的错误信息来看,可能是双引号错了。
  • 打赏
  • 举报
回复
设计一个你的业务实体类型。其中,属性(字段)是强类型的而不使用object。
Poopaye 2015-07-04
  • 打赏
  • 举报
回复
你确定是反序列化时候出的错吗? FormatException一般是string.Format的第一个参数传错了

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧