json反序列化问题

cainiaoxxx 2018-11-05 07:14:34
"{\"fire_count\":1028,\"catch_fishes\":{\"201\":[0,0,1,10],\"801\":[1,190,178,1780],\"16\":[0,0,6,60],\"18\":[1,1000,102,1020],\"301\":[4,2030,177,1770],\"608\":[0,0,1,10],\"4\":[2,80,3,30],\"3\":[1,30,3,30],\"2\":[4,80,6,60],\"1\":[2,60,9,90],\"803\":[2,820,15,150],\"703\":[1,220,14,140],\"11\":[0,0,4,40],\"21\":[3,2100,55,550],\"13\":[0,0,4,40],\"15\":[0,0,1,10],\"25\":[1,1400,194,1940],\"17\":[0,0,6,60],\"19\":[0,0,5,50],\"29\":[0,0,104,1040],\"7\":[1,70,7,70],\"6\":[1,60,20,200],\"5\":[2,100,7,70],\"705\":[1,380,10,100],\"605\":[1,200,23,230],\"216\":[1,200,14,140],\"215\":[1,160,4,40],\"8\":[0,0,1,10],\"10\":[0,0,2,20],\"9\":[1,180,8,80],\"202\":[1,210,5,50],\"206\":[0,0,1,10],\"24\":[1,1200,28,280],\"14\":[1,200,7,70],\"22\":[0,0,2,20]},\"catch_score\":10970,\"tableid\":1,\"fire_types\":{\"10\":1028},\"fire_cost\":10280}"

这串json如何反序列化
...全文
182 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
操作json通常用的类库是Newtonsoft.Json,可以通过nuget获取 将字符串变成类,叫反序列化,Newtonsoft.Json提供了一个Json基础类 如果你的json是个对象 就用JObject接收,如果是数组,就用JArray接收 Newtonsoft.Json.JsonConvert.DeserializeObject<JObject||JArray>(strJson); 如果你有自定义的类,也可以将这个类传入
stevenjin 2018-11-22
  • 打赏
  • 举报
回复
推荐用bejon这个网站生成C#实体类
小大飞 2018-11-20
  • 打赏
  • 举报
回复
2楼提供的就是正确答案。 可以对json格式化下,看起来就清晰了。
吉普赛的歌 2018-11-06
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            string strJson = "{\"fire_count\":1028,\"catch_fishes\":{\"201\":[0,0,1,10],\"801\":[1,190,178,1780],\"16\":[0,0,6,60],\"18\":[1,1000,102,1020],\"301\":[4,2030,177,1770],\"608\":[0,0,1,10],\"4\":[2,80,3,30],\"3\":[1,30,3,30],\"2\":[4,80,6,60],\"1\":[2,60,9,90],\"803\":[2,820,15,150],\"703\":[1,220,14,140],\"11\":[0,0,4,40],\"21\":[3,2100,55,550],\"13\":[0,0,4,40],\"15\":[0,0,1,10],\"25\":[1,1400,194,1940],\"17\":[0,0,6,60],\"19\":[0,0,5,50],\"29\":[0,0,104,1040],\"7\":[1,70,7,70],\"6\":[1,60,20,200],\"5\":[2,100,7,70],\"705\":[1,380,10,100],\"605\":[1,200,23,230],\"216\":[1,200,14,140],\"215\":[1,160,4,40],\"8\":[0,0,1,10],\"10\":[0,0,2,20],\"9\":[1,180,8,80],\"202\":[1,210,5,50],\"206\":[0,0,1,10],\"24\":[1,1200,28,280],\"14\":[1,200,7,70],\"22\":[0,0,2,20]},\"catch_score\":10970,\"tableid\":1,\"fire_types\":{\"10\":1028},\"fire_cost\":10280}";
            //反序列化为定义的对象
            Data obj = Newtonsoft.Json.JsonConvert.DeserializeObject<Data>(strJson);
            //再转 json , 看是否正确
            Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(obj));

            Console.Read();
        }
    }

    public class Data
    {
        public int fire_count { get; set; }
        public Dictionary<string, int[]> catch_fishes { get; set; }
        public int catch_score { get; set; }
        public int tableid { get; set; }
        public Item fire_types { get; set; }
        public int fire_cost { get; set; }
    }

    public class Item
    {
        public Dictionary<string, int> Value { get; set; }
    }
}
大鱼> 2018-11-05
  • 打赏
  • 举报
回复
嵌套了一层而已,定义好实体类,直接反序列化

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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