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"
相关推荐
Cloud_Shy61816 分钟前
解读《Effective Python 3rd Edition》:从练气到老魔(第六章 Item 40 - 43)
android·开发语言·人工智能·笔记·python·学习方法
半只小闲鱼22 分钟前
配置计划模块通用办公设备家具批复数合计计算
开发语言·python
qq_4221525744 分钟前
Word 文件太大怎么压缩?2026 年文档瘦身方案对比
开发语言·c#·word
charliedev1 小时前
Jedi:Python 自动补全与静态分析的实用工具
开发语言·python·其他
ji198594431 小时前
MATLAB 求散点曲线斜率
开发语言·算法·matlab
kaikaile19951 小时前
MATLAB 实现:Koch & Zhao 图像水印算法(DCT域)
开发语言·算法·matlab
love_muming1 小时前
链表每日一练
java·开发语言·数据结构·链表·idea·每日一练
weixin_446260851 小时前
LLM智能体在社交模拟中的决策行为分析:有限状态与LLM-based策略对比研究
开发语言·php
牛肉在哪里1 小时前
ros2 从零开始28 监听广播C++
开发语言·c++·算法·机器人
techdashen1 小时前
Cargo 1.94 开发周期全解析
开发语言·后端·rust