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"