序列化字典<;字符串,类型>;并从JSON字符串中删除键/值
本文关键字:字符串 删除 JSON 类型 lt 字典 序列化 gt 并从 | 更新日期: 2023-09-27 18:26:58
我有以下代码片段,其中我正在将对象序列化为JSON。代码运行良好,但我想从JSON字符串中删除Key
和Value
关键字。
电流输出:
{
"Description":"test",
"RoomTypes":[
{
"Key":"A",
"Value":{
"Name":"Delux"
}
},
{
"Key":"B",
"Value":{
"Name":"Non delux"
}
}
],
"Url":"http:'/'/test.com"
}
所需输出:
{
"Description":"test",
"RoomTypes":[
{
"A":{
"Name":"Delux"
}
},
{
"B":{
"Name":"Non delux"
}
}
],
"Url":"http:'/'/test.com"
}
我的测试代码:
namespace ConsoleApplication1
{
[DataContract]
public class Room
{
[DataMember]
public string Url { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public Dictionary<string, RoomTypes> RoomTypes { get; set; }
}
[DataContract]
public class RoomTypes
{
[DataMember]
public string Name { get; set; }
}
public class Test
{
static void Main(string[] args)
{
Room room = new Room { Url = "http://test.com", Description="test" };
RoomTypes roomType1 = new RoomTypes() { Name = "Delux" };
RoomTypes roomType2 = new RoomTypes() { Name = "Non delux" };
room.RoomTypes = new Dictionary<string, RoomTypes>();
room.RoomTypes["A"] = roomType1;
room.RoomTypes["B"] = roomType2;
DataContractJsonSerializer dataContractSer =
new DataContractJsonSerializer(typeof(Room));
StringBuilder sb = new StringBuilder();
MemoryStream ms = new MemoryStream();
dataContractSer.WriteObject(ms, room);
var result = Encoding.UTF8.GetString(ms.ToArray());
Console.WriteLine(result);
}
}
}
有办法做到这一点吗?
这并不有趣,但试试这个:
[Test]
public void Json()
{
var input = @"{
""Description"":""test"",
""RoomTypes"":[
{
""Key"":""A"",
""Value"":{
""Name"":""Delux""
}
},
{
""Key"":""B"",
""Value"":{
""Name"":""Non delux""
}
}
],
""Url"":""http:'/'/test.com""
}";
var temp = JsonConvert.DeserializeObject<Temp>(input);
var transform = new Transform
{
Description = temp.Description,
Url = temp.Url,
RoomTypes = new List<IDictionary<string, Temp.NameObj>>()
};
foreach (var group in temp.RoomTypes)
{
var dic = new Dictionary<string, Temp.NameObj> {{@group.Key, @group.Value}};
transform.RoomTypes.Add(dic);
}
Console.WriteLine(JsonConvert.SerializeObject(transform));
}
public class Transform
{
public string Description { get; set; }
public IList<IDictionary<string, Temp.NameObj>> RoomTypes { get; set; }
public string Url { get; set; }
}
public class Temp
{
public string Description { get; set; }
public IList<GroupObj> RoomTypes { get; set; }
public string Url { get; set; }
public class GroupObj
{
public string Key { get; set; }
public NameObj Value { get; set; }
}
public class NameObj
{
public string Name { get; set; }
}
}
这个想法是使用Json.Net的字典序列化来实现您想要的结构。
如果您使用的是.NET Framework v4.5或更高版本,您可以将DataContractJsonSerializerSettings.UseSimpleDictionaryFormat
设置为true
,以更接近您想要的格式:
var settings = new DataContractJsonSerializerSettings();
settings.UseSimpleDictionaryFormat = true;
var dataContractSer = new DataContractJsonSerializer(typeof(Room), settings);
使用带有上述修改的问题中的测试代码会产生以下输出,可能是您想要的输出(尽管它与问题中所述的期望输出不同):
{
"Description": "test",
"RoomTypes": {
"A": {
"Name": "Delux"
},
"B": {
"Name": "Non delux"
}
},
"Url": "http://test.com"
}
这是因为您使用字典来存储值。您必须编写一个自定义实现并过度编码序列化/反序列化方法
查看此处了解更多详细信息
我意识到您不想使用Json.NET,但说真的,这是为处理Json序列化而构建的库。Json.NET完全满足了您的需求,您所需要做的就是将其写成一行代码。。。
(来自文档:)
Dictionary<string, int> points = new Dictionary<string, int>
{
{ "James", 9001 },
{ "Jo", 3474 },
{ "Jess", 11926 }
};
string json = JsonConvert.SerializeObject(points, Formatting.Indented);
Console.WriteLine(json);
// {
// "James": 9001,
// "Jo": 3474,
// "Jess": 11926
// }
http://www.newtonsoft.com/json/help/html/serializedictionary.htm
您可以简单地用字符串替换函数修改您的字符串,试试这个
> public string updatejson (string jsonorg)
> {
> string updatedjson = "";
> updatedjson = jsonorg.Replace("'"Key'":", "");
> updatedjson = updatedjson.Replace("'"Value'":", "");
> updatedjson = updatedjson.Replace("'",", "'":");
> updatedjson = updatedjson.Replace("},{", ",");
> updatedjson = updatedjson.TrimStart('[');
> updatedjson = updatedjson.TrimEnd(']');
>
> return updatedjson;
> }