[英]Sort JSON response from web api
我想通过调用 Web API 对我的 JSON 响应进行排序,并且只使用响应 API 中的最高概率和 tagName。
如何在 C# 中执行此操作,还是需要使用任何库? 编辑:这是用来调用的代码我对编码很陌生,谢谢你的每一个回复。 我的程序过程是将图像数据发送到网络并接收 JSON 数据响应
{
"predictions": [
{
"**probability**": 0.15588212,
"tagId": "5ac049bf-b01e-4dc7-b613-920c75579c41",
"**tagName**": "DH246F",
"boundingBox": {
"left": 0.4654134,
"top": 0.52319163,
"width": 0.16658843,
"height": 0.20959526
}
},
{
"probability": 0.11000415,
"tagId": "5ac049bf-b01e-4dc7-b613-920c75579c41",
"tagName": "DH246F",
"boundingBox": {
"left": 0.0,
"top": 0.18623778,
"width": 0.31140158,
"height": 0.81376123
}
}
]
}
using System;
using System.Net.Http.Headers;
using System.Text;
using System.Net.Http;
using System.Web;
using System.IO;
namespace Rest_API_Test
{
static class Program
{
static void Main(string[] args)
{
string imageFilePath = @"C:\Users\wissarut.s\Desktop\Test Capture\Left.png";
MakeRequest(imageFilePath);
Console.ReadLine();
}
public static async void MakeRequest(string imageFilePath)
{
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Prediction-key", "****");
var uri = "*******************";
HttpResponseMessage response;
byte[] byteData = GetBytesFromImage(imageFilePath);
using (var content = new ByteArrayContent(byteData))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
response = await client.PostAsync(uri, content);
Console.WriteLine(await response.Content.ReadAsStringAsync());
Console.WriteLine("From Left Side Camera");
}
}
public static byte[] GetBytesFromImage(string imageFilePath)
{
FileStream fs = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
BinaryReader binReader = new BinaryReader(fs);
return binReader.ReadBytes((int)fs.Length);
}
}
}
为了对 JSON 进行排序,最好先将其反序列化为 .NET 对象,然后使用 Linq 对其进行排序。 反序列化后,内存中有内容,可以更好地处理。 如果您需要带有排序内容的 JSON,您可以在之后对其进行序列化。 第一步是定义模仿 JSON 结构的类。 您可以在 Visual Studio 中使用Edit -> Paste special -> Paste JSON as classes轻松完成此操作。
之后,您可以将 JSON 字符串反序列化为对象。 在这方面有各种框架可以帮助您,最著名的是以下框架:
在以下示例中,我自己创建了类(Paste special 创建与 .NET 命名约定冲突的小写属性名称):
public class Container
{
public IEnumerable<Prediction> Predictions { get; set; }
}
public class Prediction
{
public decimal Probability { get; set; }
public Guid TagId { get; set; }
public string TagName { get; set; }
public BoundingBox BoundingBox { get; set; }
}
public class BoundingBox
{
public decimal Left { get; set; }
public decimal Top { get; set; }
public decimal Width { get; set; }
public decimal Height { get; set; }
}
之后,我将您的 JSON 存储在一个字符串中(但您可以使用从 POST 收到的字符串)并使用 System.Text.Json 在对象结构中反序列化它:
var options = new JsonSerializerOptions()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
var container = JsonSerializer.Deserialize<Container>(json, options);
上面的代码配置了选项,以便在读取时将骆驼大小写应用于属性名称。 因此,小写 JSON 属性名称正确映射到 .NET 类中的大写属性名称。
可以使用 Linq 方法OrderByDescending
和ThenBy
轻松对预测进行排序:
container.Predictions = container.Predictions
.OrderByDescending(x => x.Probability)
.ThenBy(x => x.TagName);
结果是以下顺序:
0.15588212, DH246F, 5ac049bf-b01e-4dc7-b613-920c75579c41
0.11000415, DH246F, 5ac049bf-b01e-4dc7-b613-920c75579c41
要将对象结构再次序列化为 JSON,可以使用此方法:
var serialized = JsonSerializer.Serialize(container, options);
如果您没有大量数据,则此方法将起作用,如果您处理 Web 请求的结果,通常情况并非如此。
请参阅此小提琴以检查示例。
您可以在此处找到 System.Net.Json 的概述。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.