gpt4 book ai didi

c# - 自定义从 Web API 接收的 JSON

转载 作者:行者123 更新时间:2023-11-30 21:44:02 28 4
gpt4 key购买 nike

我需要在 Json 字符串中添加一个节点。

以下是我读取数据的代码。

var url = "https://xyz_12232_abc/0908978978979.json";
var sys = new WebClient();
var content = sys.DownloadString(url);

我从上面的代码中收到以下输出:

{
"2312312312313":
{
"emailId":"abc@gmail.com",
"model":"XYZ001",
"phone":"+654784512547",
"userName":"User1"
},
"23456464512313":
{
"emailId":"abcd@gmail.com",
"model":"XYZ002",
"phone":"+98745114474",
"userName":"User2"
},
"45114512312313":
{
"emailId":"abcde@gmail.com",
"model":"XYZ3",
"phone":"+214784558741",
"userName":"User3"
}
}

但是,我想要这样的输出:

{
"Records": [
{
"UID":"2312312312313":,
"emailId":"abc@gmail.com",
"model":"XYZ001",
"phone":"+654784512547",
"userName":"User1"
},

{
"UID":"23456464512313":,
"emailId":"abcd@gmail.com",
"model":"XYZ002",
"phone":"+98745114474",
"userName":"User2"
},

{
"UID":"45114512312313":,
"emailId":"abcde@gmail.com",
"model":"XYZ3",
"phone":"+214784558741",
"userName":"User3"
}
]
}

现在,如何实现?

最佳答案

您可以使用 Json.NET将数据按摩到您想要的输出中:

var jsonStr = @"..."; // your JSON here
var obj = JsonConvert.DeserializeObject<Dictionary<string, JObject>>(jsonStr);

var formattedObj = new
{
Records = obj.Select(x =>
{
x.Value.AddFirst(new JProperty("UID", x.Key));
return x.Value;
})
};

// serialize back to JSON
var formattedJson = JsonConvert.SerializeObject(formattedObj);

关于c# - 自定义从 Web API 接收的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41096047/

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