简体   繁体   English

如何使用 C# 将列表转换为 json

[英]How do I convert list to json using C#

I need to convert storage queue messages as per the following sample to json.我需要按照以下示例将存储队列消息转换为 json。

DeviceId: "a4592037"
DeviceName: "device3"
FirmwareVersion: "0.1.23"
MfgDate: "01/02/208"
Level: "normal"
DeviceUptime: "323234"

Anyone have a suggestion?有人有建议吗?

Try out the JSON library from Newtonsoft.试用 Newtonsoft 的 JSON 库。 With that you can just use:这样你就可以使用:

JsonConvert.SerializeObject(YourObject)

Details at: https://www.newtonsoft.com/json详情请见: https : //www.newtonsoft.com/json

NOTE: The data provided by the user is in plain text and not an object model, and as such will return new line characters.注意:用户提供的数据是纯文本而不是对象模型,因此将返回换行符。 To Combat this:为了解决这个问题:

string convertMeDaddy = JsonConvert.SerializeObject(YourObject) convertMeDaddy = convertMeDaddy.replace(System.Environment.NewLine, "");

(the above is if it uses the new line indicator, otherwise change system.enviroment.newline to "\\\\r\\n" (以上是如果它使用换行符,否则将 system.enviroment.newline 更改为 "\\\\r\\n"

I think this will work:我认为这会奏效:

var temp = JsonConvert.DeserializeObject<dynamic>(myQueueItem);

var device = new Devices
    {
    DeviceId = temp.DeviceId,
    DeviceName = temp.DeviceName,
    DeviceUptime = temp.DeviceUptime,
    FirmwareVersion = temp.FirmwareVersion,
    Level = temp.Level,
    MfgDate = temp.MfgDate
    };

 var data = JsonConvert.SerializeObject(device);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM