[英]Returning JSON Array of Objects in C#
These are the classes I have which represent the way I want the data to be shown to the end user: 这些是我拥有的类,它们代表我希望将数据显示给最终用户的方式:
namespace PHCTimeSeriesInsights.API.Models
{
public class ProductTimeSeries
{
public List<DeviceTimeSeries> DeviceTimeSeries { get; set; }
public string DeviceId { get; set; }
}
public class DeviceTimeSeries
{
public DateTime TimeStamp { get; set; }
public Double MeasuredValue { get; set; }
public string DateField { get; set; }
}
}
I want the output in the following format: Here the string 8787887 is an actual value of a device; 我想要以下格式的输出:这里的字符串8787887是设备的实际值;
{
"8787887":[
{
"prop":"val",
"prop":"val"
},
{
"prop":"val1",
"prop":"val2"
}
]
}
I've this snippet representing what I did but it does not give me an optimal format: 我的这段代码代表了我所做的事情,但是并没有给我一种最佳格式:
List<ProductTimeSeries> prd = new List<ProductTimeSeries>();
prd.Add(new ProductTimeSeries
{
DeviceId = getTelemetry.PropertyName,
DeviceTimeSeries = deviceIdTimeSeries
});
This is the result that I get: 这是我得到的结果:
[
{
"deviceTimeSeries": [
{
"timeStamp": "2019-07-19T17:44:55.856Z",
"measuredValue": 6.98,
"dateField": "7/19/2019"
},
{
"timeStamp": "2019-07-19T17:45:58.697Z",
"measuredValue": 7.04,
"dateField": "7/19/2019"
}
],
"deviceId": "deviceId"
}
]
What changes do I have to make to the format in the way I want? 我需要对格式进行哪些更改?
Instead of using classes, in C# you want to actually use a Dictionary. 在C#中,您实际上要使用Dictionary,而不是使用类。 In your case, it should work by using a
Dictionary<string, List<DeviceTimeSeries>>
where the string
is your “DeviceId”. 在您的情况下,它应该通过使用
Dictionary<string, List<DeviceTimeSeries>>
,其中string
是您的“ DeviceId”。
See the example below. 请参见下面的示例。
https://dotnetfiddle.net/luE1Z4 https://dotnetfiddle.net/luE1Z4
Which in sort runs 哪个运行
Console.WriteLine("Dictionary Test");
var productTimeSeries2 = new Dictionary<string, IList<DeviceTimeSeries>> () {
{
"DeviceId-888",
new List < DeviceTimeSeries > () {
new Program.DeviceTimeSeries() {
TimeStamp = DateTime.Now,
MeasuredValue = 2,
DateField = "7/29/2019"
}
}
}
};
Console.WriteLine(JsonConvert.SerializeObject(productTimeSeries2));
And outputs 和输出
{
"DeviceId-888":[
{
"TimeStamp":"2019-07-31T20:45:28.677905+00:00",
"MeasuredValue":2.0,
"DateField":"7/29/2019"
}
]
}
List prd 清单prd
needs to be 需要是
Dictionary> prd 字典> prd
then your object should serialize to your desired format. 那么您的对象应序列化为所需的格式。
It's not clear what you want to put in the values of prop
and value
尚不清楚您要在
prop
和value
的值中放入什么
But I believe the upshot is you will have to use some sort of custom code to write out your structure. 但是我相信结果是您将必须使用某种自定义代码来写出您的结构。 Below I use a custom
JsonConverter
from Json.Net 下面我用一个自定义
JsonConverter
从Json.Net
public class CustomJsonConverter : JsonConverter
{
private readonly Type[] _types;
public CustomJsonConverter(params Type[] types)
{
_types = types;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
JToken t = JToken.FromObject(value);
if (t.Type != JTokenType.Object)
{
t.WriteTo(writer);
}
else
{
JObject o = (JObject)t;
IList<string> propertyNames = o.Properties().Select(p => p.Name).ToList();
JObject root = new JObject();
JObject props = JObject.Parse(@"{""prop"" : ""A"", ""value"":""B""}");
root.AddFirst(new JProperty(o["DeviceId"].Value<object>().ToString(), props));
root.WriteTo(writer);
}
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
}
public override bool CanRead
{
get { return false; }
}
public override bool CanConvert(Type objectType)
{
return _types.Any(t => t == objectType);
}
}
Then, serialize your object using the custom converter 然后,使用自定义转换器序列化对象
DeviceTimeSeries d = new DeviceTimeSeries { TimeStamp = DateTime.Now, MeasuredValue = 1.23, DateField = "01/01/1970" };
ProductTimeSeries p = new ProductTimeSeries();
List<DeviceTimeSeries> dts = new List<DeviceTimeSeries>();
dts.Add(d);
p.DeviceTimeSeries = dts;
p.DeviceId = "8787887";
string json = JsonConvert.SerializeObject(p, Formatting.Indented, new CustomJsonConverter(typeof(ProductTimeSeries)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.