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"
相关推荐
hdsoft_huge6 小时前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot
风中的微尘6 小时前
39.网络流入门
开发语言·网络·c++·算法
未来之窗软件服务7 小时前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
小冯记录编程7 小时前
C++指针陷阱:高效背后的致命危险
开发语言·c++·visual studio
1uther7 小时前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
C_Liu_8 小时前
C++:类和对象(下)
开发语言·c++
coderxiaohan8 小时前
【C++】类和对象1
java·开发语言·c++
阿幸软件杂货间8 小时前
Office转PDF转换器v1.0.py
开发语言·pdf·c#
扯淡的闲人8 小时前
多语言编码Agent解决方案(5)-IntelliJ插件实现
开发语言·python
丑小鸭是白天鹅9 小时前
Kotlin协程详细笔记之切线程和挂起函数
开发语言·笔记·kotlin