gpt4 book ai didi

.net - 在 .Net 中解析自定义 JSON 对象?

转载 作者:行者123 更新时间:2023-12-02 08:57:39 25 4
gpt4 key购买 nike

对于我的 ASP.Net 应用程序,我需要使用返回 JSON 对象的 HTTP REST API。JSON 的外观如下:

message: [
{
type: "message"
href: "/messages/id/23"
view_href: /this-is-a-test-message/m-p/23
id: {
type: "int"
$: 23
}
},
{ //...
}
]

我可以使用 DataContractJsonSerializer 类并定义自定义类。但是,我不确定像“$”这样的键在 .Net 中如何转换为。

有什么建议吗?

最佳答案

这看起来就像你想要的那样,JSON.NET :

using System;
using Newtonsoft.Json.Linq;

class Test
{
static void Main(string[] args)
{
JObject top = new JObject(
new JProperty("type", "message"),
new JProperty("href", "/messages/id/23"),
new JProperty("view_href", "/this-is-a-test-message/m-p/23"),
new JProperty("id", new JObject(new JProperty("type", "int"),
new JProperty("$", 23))));

Console.WriteLine(top);

// Round trip
string json = top.ToString();
JObject reparsed = JObject.Parse(json);
}
}

关于.net - 在 .Net 中解析自定义 JSON 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3709430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com