如何将集合序列化为具有重复“密钥”名称的JSON? [英] How do I serialize a collection to JSON which has duplicate 'key' names?
问题描述
大家好。我是一个使用JSON序列化的新手,所以任何帮助将不胜感激!另外,我的智慧结束了。我正在使用标准的Newtonsoft JSON序列化程序,我需要以某种方式将我的directoryIds属性序列化为完全,如下面的结果#2。目前,我正在获得结果#1。我检查以确保两者都是使用此Web验证器的有效JSON格式: http://jsonformatter.curiousconcept .com / [ ^ ]。我只发布了一个部分JSON对象供参考。
Hello all. I'm kind of a newbie using JSON serialization, so any help would be greatly appreciated! Plus, I'm at my wits end. I'm using the standard Newtonsoft JSON serializer and I need to somehow get my directoryIds property to serialize exactly like result #2 below. Currently, I'm getting result #1. I checked to make sure both were valid JSON formats using this Web validator : http://jsonformatter.curiousconcept.com/[^]. I've only posted a partial JSON object for reference.
...
[DataMember]
public DirectoryId[] directoryIds { get; set; }
...
[DataContract]
public class DirectoryId
{
[DataMember]
[JsonProperty(PropertyName = "string")]
public string Str { get; set; }
}
以下是序列化对象的代码,以防它相关:
Here's the code to serialize the object just in case it's pertinent :
public class RestSharpJsonSerializer : ISerializer
{
public string Serialize(object obj)
{
return JsonConvert.SerializeObject(obj, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.Indented });
}
public string RootElement { get; set; }
public string Namespace { get; set; }
public string DateFormat { get; set; }
public string ContentType
{
get { return "application/json"; }
set { }
}
}
结果#1
Result #1
"searchScope":"IndividualProvidersOnly",<br />
"directoryIds":[<br />
{<br />
"string":"NGX"<br />
},<br />
{<br />
"string":"MDA"<br />
}<br />
],<br />
"resultSizeLimit":10,
结果#2
Result #2
"searchScope":"IndividualProvidersOnly",<br />
"directoryIds":[<br />
{<br />
"string":"NGX",<br />
"string":"MDA"<br />
}<br />
],<br />
"resultSizeLimit":10,
本周结束这真是让我烦恼所以我'我期待着接下来几天能得到的任何帮助。谢谢大家!
推荐答案
这篇关于如何将集合序列化为具有重复“密钥”名称的JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!