C# json处理

用到的库CsharpJson

JsonDocument doc = JsonDocument.FromString(e.Data);
//将json数据转换
// {"id":2386034,"roomID":4719881,"title":"舰长","type":"guard","cmd":"lottery"}
if (doc.IsObject())
{
JsonObject jsobj = doc.Object;
if (jsobj.Value("id").Valuetype == JsonType.STRING)
{
Console.WriteLine(" ID: " + jsobj.Value("id"));
}
else
{
Console.WriteLine(" ID: " + jsobj.Value("id").ToInt());
}

Console.WriteLine(" roomID: " + jsobj.Value("roomID").ToInt());
Console.WriteLine(" Title: " + jsobj.Value("title"));
Console.WriteLine(" Type: " + jsobj.Value("type"));
}

最后返回的数据

 ID: 2386034
roomID: 4719881
Title: 舰长
Type: guard



记性不好记录一下。

Last modification:June 9th, 2020 at 06:14 pm
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment