使用Newtonsoft.Json进行反序列化的问题,不知道如何解析这段

lhp_sybase 2014-04-15 09:05:05
示例JSON字符串:
"anumber": {
"24": [204],
"27": [423],
"68": [928],
"38": [499, 295, ]
}
-------------------------------------
说明:"24","27","68","38" 都是不确定的,[]里面的为数组。
请教诸位,这个要咋读取?主要是这个不确定的属性,没法定义个类。

...全文
223 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lhp_sybase 2014-04-15
  • 打赏
  • 举报
回复
@fangxinggood 我的JSON确实没贴全,复制的时候漏掉了。不过其实主要是想表达如何读取不确定属性的方法,非常感谢!!!
引用 7 楼 fangxinggood 的回复:
转成 Dictionary<string, int[]> , 另外你的json格式不对啊,最外面应该是 {}

   class Program
    {
        static void Main(string[] args)
        {
            var json = System.IO.File.ReadAllText("1.txt");
            var result = JsonConvert.DeserializeObject<Obj>(json);
            result.Anumber.ToList().ForEach(x => Console.WriteLine("key:{0},value:{1}", x.Key, x.Value));
            Console.Read();
        }
    }

    class Obj
    {
        public Dictionary<string, int[]> Anumber {get; set;}
    }
----------------------已测试,使用 Dictionary<string, int[]>确实方便了很多。非常感谢!!!--------------------------------
机器人 2014-04-15
  • 打赏
  • 举报
回复
转成 Dictionary<string, int[]> , 另外你的json格式不对啊,最外面应该是 {}

   class Program
    {
        static void Main(string[] args)
        {
            var json = System.IO.File.ReadAllText("1.txt");
            var result = JsonConvert.DeserializeObject<Obj>(json);
            result.Anumber.ToList().ForEach(x => Console.WriteLine("key:{0},value:{1}", x.Key, x.Value));
            Console.Read();
        }
    }

    class Obj
    {
        public Dictionary<string, int[]> Anumber {get; set;}
    }
lhp_sybase 2014-04-15
  • 打赏
  • 举报
回复
引用 5 楼 wind_cloud2011 的回复:
string jj = "{\"anumber\":{\"24\": [204],\"27\": [423],\"68\": [928],\"38\": [499, 295,]}}"; JObject jo = JObject.Parse(jj); string[] values = jo.Properties().Select(item => item.Value.ToString()).ToArray(); JObject jo1 = JObject.Parse(values[0]); string[] values1 = jo1.Properties().Select(item => item.Value.ToString()).ToArray(); MessageBox.Show(values1[0]);
-----------------------已测试,5L正解,非常感谢!!!---------------------------------
wind_cloud2011 2014-04-15
  • 打赏
  • 举报
回复
string jj = "{\"anumber\":{\"24\": [204],\"27\": [423],\"68\": [928],\"38\": [499, 295,]}}"; JObject jo = JObject.Parse(jj); string[] values = jo.Properties().Select(item => item.Value.ToString()).ToArray(); JObject jo1 = JObject.Parse(values[0]); string[] values1 = jo1.Properties().Select(item => item.Value.ToString()).ToArray(); MessageBox.Show(values1[0]);
t1039734884 2014-04-15
  • 打赏
  • 举报
回复
通过 JSON.parse(anumber)
全栈极简 2014-04-15
  • 打赏
  • 举报
回复
我看还不如直接取"{"和"}"之间的内容然后以","分割来的直接呢。

110,502

社区成员

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

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

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