我是json的新手,我需要帮助跟随json字符串,我需要将值提取到字符串变量中以保存到db。
{
"message_id": {"8624389": "447123456789"},
"sent_text": "Test message",
"parts_count": 1
}
如何将message_id,sent_text和parts_count的值变为变量?
谢谢
答案 0 :(得分:0)
我使用这个网站:https://app.quicktype.io/ 它使用newtonsoft nuget包。稍加调整就可以节省大量时间。
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
// using MyNamespace;
//
// var myObject = MyObject.FromJson(jsonString);
namespace MyNamespace
{
using System;
using System.Net;
using System.Collections.Generic;
using Newtonsoft.Json;
public partial class MyObject
{
[JsonProperty("message_id")]
public MessageId MessageId { get; set; }
[JsonProperty("sent_text")]
public string SentText { get; set; }
[JsonProperty("parts_count")]
public long PartsCount { get; set; }
}
public partial class MessageId
{
[JsonProperty("8624389")]
public string The8624389 { get; set; }
}
public partial class MyObject
{
public static MyObject FromJson(string json) => JsonConvert.DeserializeObject<MyObject>(json, MyNamespace.Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this MyObject self) => JsonConvert.SerializeObject(self, MyNamespace.Converter.Settings);
}
public class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
};
}
}