zoukankan      html  css  js  c++  java
  • json.net 字典json字符串相互转换

    参考官方连接:

    Deserialize a Dictionary (newtonsoft.com)

    步骤1:vs 项目安装对应包

    步骤2: 添加using 引用 using Newtonsoft.Json;

    完整程序

    程序代码:

     1 using System;
     2 using System.Collections.Generic;
     3 using Newtonsoft.Json;
     4 
     5 namespace ConsoleApp5
     6 {
     7     class Program
     8     {
     9         static void Main(string[] args)
    10         {
    11             /* 字典序列化到json字符串 */
    12             //创建字典对象
    13             Dictionary<string, int> points = new Dictionary<string, int>
    14             {
    15                 { "James", 9001 },
    16                 { "Jo", 3474 },
    17                 { "Jess", 11926 }
    18             };
    19             //将字典对象转换为 json字符串
    20             string json = JsonConvert.SerializeObject(points, Formatting.Indented);
    21             //终端打印显示
    22             Console.WriteLine("字典转json串-->" + json);
    23 
    24             /* json 字符串转换到字典对象 */
    25             Dictionary<string, int> read = JsonConvert.DeserializeObject<Dictionary<string, int>>(json);
    26             //打印字典指定KEY值
    27             Console.WriteLine("json转字典-->James: " + read["James"]);
    28 
    29             //等待按键输入,使得中断窗口悬停
    30             Console.ReadKey();
    31         }
    32     }
    33 }

    终端打印输出:

  • 相关阅读:
    一周内签到连续天数求解
    int型动态数组总结
    快速排序总结
    希尔排序总结
    冒泡排序的总结
    桶排序总结
    插入排序的总结
    选择排序的总结
    二分法的五种实现
    安装Yum源
  • 原文地址:https://www.cnblogs.com/chenxiaolinembed/p/15123304.html
Copyright © 2011-2022 走看看