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"
相关推荐
软件黑马王子3 小时前
Unity游戏制作中的C#基础(5)条件语句和循环语句知识点全解析
游戏·unity·c#
shepherd枸杞泡茶3 小时前
第3章 3.3日志 .NET Core日志 NLog使用教程
c#·asp.net·.net·.netcore
Biomamba生信基地3 小时前
两天入门R语言,周末开讲
开发语言·r语言·生信
子非衣3 小时前
MySQL修改JSON格式数据示例
android·mysql·json
RAN_PAND3 小时前
STL介绍1:vector、pair、string、queue、map
开发语言·c++·算法
Bio Coder3 小时前
R语言安装生物信息数据库包
开发语言·数据库·r语言
Tiger Z3 小时前
R 语言科研绘图第 27 期 --- 密度图-分组
开发语言·程序人生·r语言·贴图
life_time_6 小时前
C语言(22)
c语言·开发语言
Minner-Scrapy6 小时前
DApp 开发入门指南
开发语言·python·web app