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"
相关推荐
我不会编程5554 小时前
Python Cookbook-5.1 对字典排序
开发语言·数据结构·python
李少兄4 小时前
Unirest:优雅的Java HTTP客户端库
java·开发语言·http
“抚琴”的人4 小时前
【机械视觉】C#+VisionPro联合编程———【六、visionPro连接工业相机设备】
c#·工业相机·visionpro·机械视觉
无名之逆5 小时前
Rust 开发提效神器:lombok-macros 宏库
服务器·开发语言·前端·数据库·后端·python·rust
似水এ᭄往昔5 小时前
【C语言】文件操作
c语言·开发语言
啊喜拔牙5 小时前
1. hadoop 集群的常用命令
java·大数据·开发语言·python·scala
xixixin_5 小时前
为什么 js 对象中引用本地图片需要写 require 或 import
开发语言·前端·javascript
W_chuanqi5 小时前
安装 Microsoft Visual C++ Build Tools
开发语言·c++·microsoft
anlogic5 小时前
Java基础 4.3
java·开发语言
A旧城以西6 小时前
数据结构(JAVA)单向,双向链表
java·开发语言·数据结构·学习·链表·intellij-idea·idea