C# newtonsoft将json对象以字符串形式显示或者字典形式展示

cs 复制代码
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace NewtonsoftTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var res = @"{
  ""response_biz_content"": {
    ""ecoupon_amt"": ""0"",
    ""tp_cust_id"": """",
    ""openId"": """",
    ""channel"": ""0"",
    ""return_msg"": ""没有信息"",
    ""total_amt"": ""0"",
    ""pay_status"": ""2"",
    ""card_no"": """",
    ""bank_disc_amt"": ""0"",
    ""bank_name"": """",
    ""trx_ser_no"": """",
    ""mer_disc_amt"": ""0"",
    ""attach"": """",
    ""msg_id"": """",
    ""point_amt"": ""0"",
    ""bank_type"": """",
    ""total_disc_amt"": ""0"",
    ""pay_time"": """",
    ""out_trade_no"": """",
    ""coupon_amt"": ""0"",
    ""sub_open_id"": """",
    ""tp_order_id"": """",
    ""buyer_logon_id"": """",
    ""payment_amt"": ""0"",
    ""return_code"": ""00095024"",
    ""cust_id"": """",
    ""order_id"": ""20240529155214""
  },
  ""sign"": ""abcde""
}";
            // 解析JSON字符串为JObject  
            JObject jsonObject = JObject.Parse(res);
            // 获取response_biz_content的值并序列化为字符串  
            string responseBizContentStr = jsonObject["response_biz_content"].ToString(Formatting.Indented);
            Console.WriteLine(responseBizContentStr);
        }
    }
}

结果如下

json 复制代码
{
  "ecoupon_amt": "0",
  "tp_cust_id": "",
  "openId": "",
  "channel": "0",
  "return_msg": "没有信息",
  "total_amt": "0",
  "pay_status": "2",
  "card_no": "",
  "bank_disc_amt": "0",
  "bank_name": "",
  "trx_ser_no": "",
  "mer_disc_amt": "0",
  "attach": "",
  "msg_id": "",
  "point_amt": "0",
  "bank_type": "",
  "total_disc_amt": "0",
  "pay_time": "",
  "out_trade_no": "",
  "coupon_amt": "0",
  "sub_open_id": "",
  "tp_order_id": "",
  "buyer_logon_id": "",
  "payment_amt": "0",
  "return_code": "00095024",
  "cust_id": "",
  "order_id": "20240529155214"
}

根据上面的思路可以将对象转化为字典类型Dictionary<string,string>

cs 复制代码
var dic = new Dictionary<string, string>();
foreach (var jobj in jsonObject)
{
    dic.Add(jobj.Key,jsonObject[jobj.Key].ToString(Formatting.Indented));
}

foreach (var dicValue in dic)
{
    Console.WriteLine(dicValue.Key);
    Console.WriteLine(dicValue.Value);
}

结果

bash 复制代码
response_biz_content
{
  "ecoupon_amt": "0",
  "tp_cust_id": "",
  "openId": "",
  "channel": "0",
  "return_msg": "没有信息",
  "total_amt": "0",
  "pay_status": "2",
  "card_no": "",
  "bank_disc_amt": "0",
  "bank_name": "",
  "trx_ser_no": "",
  "mer_disc_amt": "0",
  "attach": "",
  "msg_id": "",
  "point_amt": "0",
  "bank_type": "",
  "total_disc_amt": "0",
  "pay_time": "",
  "out_trade_no": "",
  "coupon_amt": "0",
  "sub_open_id": "",
  "tp_order_id": "",
  "buyer_logon_id": "",
  "payment_amt": "0",
  "return_code": "00095024",
  "cust_id": "",
  "order_id": "20240529155214"
}
sign
"abcde"
相关推荐
你怎么知道我是队长5 小时前
C语言---头文件
c语言·开发语言
期待のcode5 小时前
Java虚拟机的运行模式
java·开发语言·jvm
我是唐青枫5 小时前
C#.NET ConcurrentDictionary<TKey, TValue> 深度解析:原理与实践
c#·.net
hqwest5 小时前
码上通QT实战25--报警页面01-报警布局设计
开发语言·qt·qwidget·ui设计·qt布局控件
a程序小傲5 小时前
京东Java面试被问:动态规划的状态压缩和优化技巧
java·开发语言·mysql·算法·adb·postgresql·深度优先
HellowAmy5 小时前
我的C++规范 - 玩一个小游戏
开发语言·c++·代码规范
徐先生 @_@|||6 小时前
Palantir Foundry 五层架构模型详解
开发语言·python·深度学习·算法·机器学习·架构
tang777897 小时前
爬虫如何绕过绕过“5秒盾”Cloudflare:从浏览器指纹模拟到Rust求解之不完全指南
开发语言·爬虫·rust·cloudflare
Yuer20257 小时前
什么是 Rust 语境下的“量化算子”——一个工程对象的最小定义
开发语言·后端·rust·edca os·可控ai
hqwest7 小时前
码上通QT实战26--系统设置01-系统设置布局
开发语言·qt·qss·qt基础控件·qt布局控件·qt表格控件