此代码是ASP.Net Web API控制器的一部分,我有一个特殊要求,即在将其发送到UI之前修改最终Json中的某些值
我目前的代码如下:
// Web API controller
public partial class ViewRController
{
[HttpGet]
public DataMapList FetchEmployeeDataList()
{
DataMapList returnDataMapList = new DataMapList();
return (returnDataMapList);
}
}
// Type definition and details
[JsonObject]
[JsonConverter(typeof(DataMapListSerializer))]
public class DataMapList
{
public List<Dictionary<object, object>> employeeSalaryMappingList = null;
public DataMapList()
{
employeeSalaryMappingList = new List<Dictionary<object, object>>();
employeeSalaryMappingList.Add(new Dictionary<object, object>());
employeeSalaryMappingList[0].Add(1, 10000);
employeeSalaryMappingList[0].Add(2, 13000);
employeeSalaryMappingList[0].Add(3, 15000);
employeeSalaryMappingList.Add(new Dictionary<object, object>());
employeeSalaryMappingList[1].Add(4, 9000);
employeeSalaryMappingList[1].Add(5, 12000);
employeeSalaryMappingList[1].Add(6, 11000);
}
}
// Custom Json serializer
public class DataMapListSerializer : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var employeeSalaryDataMapList = value as DataMapList;
var employeeSalaryMappingList = employeeSalaryDataMapList.employeeSalaryMappingList;
writer.WriteStartObject();
foreach (var employeeSalaryMappingDictionary in employeeSalaryMappingList)
{
writer.WritePropertyName("ED");
writer.WriteStartObject();
foreach (var keyValuePair in employeeSalaryMappingDictionary)
{
writer.WritePropertyName("ID-" + keyValuePair.Key);
serializer.Serialize(writer, keyValuePair.Value);
}
writer.WriteEndObject();
}
writer.WriteEndObject();
}
public override bool CanConvert(Type objectType)
{
return typeof(DataMapList).IsAssignableFrom(objectType);
}
}
我面临的问题是我的结果:
{
"ED": { "ID-1": 10000, "ID-2": 13000, "ID-3": 15000 },
"ED": { "ID-4": 9000, "ID-5": 12000, "ID-6": 11000 }
}
但是我希望得到以下结果,因为我正在序列化一个集合(.Net List类型,.Net集合)
[
"ED": { "ID-1": 10000, "ID-2": 13000, "ID-3": 15000 },
"ED": { "ID-4": 9000, "ID-5": 12000, "ID-6": 11000 }
]
实际上当我尝试在Json Lint中验证当前的结果时,它显示了有效的Json但终止了它的一部分,因此它将以下显示为有效的Json,但这只是一个转移< / p>
{
"ED": {
"ID-4": 9000,
"ID-5": 12000,
"ID-6": 11000
}
}
我尝试将writer.WriteStartObject();
和writer.WriteEndObject();
替换为writer.WriteStartArray();
和writer.WriteEndArray();
,然后在第一个遍历类型中的List的foreach循环之前,但这会导致例外,所以没有帮助
"exceptionMessage": "Token StartArray in state ObjectStart would result in an invalid JSON object. Path '
如果您需要任何澄清,任何解决问题的建议都会告诉我
答案 0 :(得分:4)
在JSON中,名称 - 值对必须位于对象{}
内;它们不能直接在数组[]
中。因此,您尝试创建的此JSON无效,这就是您获得异常的原因:
[
"ED": { "ID-1": 10000, "ID-2": 13000, "ID-3": 15000 },
"ED": { "ID-4": 9000, "ID-5": 12000, "ID-6": 11000 }
]
为了使它成为一个有效的集合,每个ED
名称 - 值对必须包装在这样的对象中:
[
{
"ED": { "ID-1": 10000, "ID-2": 13000, "ID-3": 15000 }
},
{
"ED": { "ID-4": 9000, "ID-5": 12000, "ID-6": 11000 }
}
]
一旦看到,解决方案就变得清晰:在DataMapListSerializer
中,您需要在循环外使用writer.WriteStartArray()
和writer.WriteEndArray()
,并且需要添加writer.WriteStartObject()
和在你的循环中writer.WriteEndObject()
。
writer.WriteStartArray();
foreach (var employeeSalaryMappingDictionary in employeeSalaryMappingList)
{
writer.WriteStartObject();
writer.WritePropertyName("ED");
writer.WriteStartObject();
foreach (var keyValuePair in employeeSalaryMappingDictionary)
{
writer.WritePropertyName("ID-" + keyValuePair.Key);
serializer.Serialize(writer, keyValuePair.Value);
}
writer.WriteEndObject();
writer.WriteEndObject();
}
writer.WriteEndArray();