我有以下代码片段,其中我将对象序列化为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);
}
}
}
有办法做到这一点吗?
答案 0 :(得分:2)
这不好玩,但试试这个:
[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的字典序列化来实现你想要的结构。
答案 1 :(得分:1)
如果您使用的是.NET Framework v4.5或更高版本,则可以将DataContractJsonSerializerSettings.UseSimpleDictionaryFormat
设置为true
以更接近您想要的格式:
var settings = new DataContractJsonSerializerSettings();
settings.UseSimpleDictionaryFormat = true;
var dataContractSer = new DataContractJsonSerializer(typeof(Room), settings);
使用上述修改的问题中的测试代码会产生以下输出,可能正在寻找(尽管它与问题中所述的所需输出不同):< / p>
{
"Description": "test",
"RoomTypes": {
"A": {
"Name": "Delux"
},
"B": {
"Name": "Non delux"
}
},
"Url": "http://test.com"
}
答案 2 :(得分:0)
因为你使用字典来存储值。您必须编写自定义实现并覆盖序列化/反序列化方法
查看here了解更多详情
答案 3 :(得分:0)
我意识到你不想使用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
答案 4 :(得分:-2)
只需用字符串替换函数修改你的字符串试试这个
> 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;
> }