[英]How to convert json array of values to nested json objects in C#
I have a JSON file and one of the values is represented as a list of integers, but I would like to convert the list to nested JSONS with its value and surrogate key (autoincrement from 1 for each list). 我有一个JSON文件,其中一个值表示为整数列表,但我想将列表及其值和代理键(每个列表从1自动递增)转换为嵌套JSONS。
Is it possible to use Newtonsoft.json.dll for this task? 是否可以将Newtonsoft.json.dll用于此任务?
This is actual example 这是实际的例子
{
"content": {
"table": [
{
"name": {
"en": "questionnaire"
},
"data": [
"154",
"124254",
"87575"
]
}
]
}
}
This is needed output: 这是必需的输出:
{
"content": {
"table": [
{
"name": {
"en": "questionnaire"
},
"data": [
{
"id": "1",
"value": "154"
},
{
"id": "2",
"value": "124254"
},
{
"id": "3",
"value": "87575"
}
]
}
]
}
}
Thank you very much for any ideas. 非常感谢您的任何想法。
If this gives you some idea: 如果这给您一些想法:
var json = "{'data': ['154','124254','87575']}";
dynamic obj = JsonConvert.DeserializeObject(json);
for (int i = 0; i < obj.data.Count; i++)
{
dynamic val = new { id = i + 1, value = obj.data[i] };
obj.data[i] = JsonConvert.SerializeObject(val);
}
var newJson = JsonConvert.SerializeObject(obj);
You can create a custom converter which is responsible for converting data
array into a new dictionary<string, string>
. 您可以创建一个自定义转换器,该转换器负责将
data
数组转换为新的dictionary<string, string>
。 The custom converter looks like below: 自定义转换器如下所示:
public class DataConverter : JsonConverter
{
private int _index = 1;
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
JToken token = JToken.FromObject(value);
token.WriteTo(writer);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken token = JToken.Load(reader);
Dictionary<string, string> dict = new Dictionary<string, string>();
try
{
foreach (var item in token)
{
dict.Add(_index++.ToString(), item.ToString());//Filling the data to dictionary
}
}
catch
{
// ignored
}
_index = 1;//Resetting index
return dict;
}
public override bool CanConvert(Type objectType)
{
return objectType.IsAssignableFrom(typeof(IDictionary<string, string>));
}
}
Then you should have the models as below in order to fetch the json into strongly typed objects. 然后,您应该具有以下模型,以便将json提取到强类型对象中。 As u can see there is this attribute
[JsonConverter(typeof(DataConverter))]
which tells the converter how to serialize/deserialize the content. 如您所见,此属性
[JsonConverter(typeof(DataConverter))]
告诉转换器如何对内容进行序列化/反序列化。
[Serializable]
public class Table
{
[JsonProperty("data")]
[JsonConverter(typeof(DataConverter))]
public Dictionary<string, string> Data { get; set; }
}
[Serializable]
public class Content
{
[JsonProperty("table")]
public Table[] Tables { get; set; }
}
[Serializable]
public class MyObject
{
[JsonProperty("content")]
public Content Content { get; set; }
}
Here is a simple console app to test it: - be aware that i removed everything else under table for the sake of simplicity but you can add them in your app. 这是一个测试它的简单控制台应用程序:-请注意,为简单起见,我删除了表格下的所有其他内容,但您可以在应用程序中添加它们。
public static void Main()
{
string content = @"{
""content"": {
""table"": [
{
""data"": [
""154"",
""124254"",
""87575""
]
}
]
}
}";
var result = JsonConvert.DeserializeObject<MyObject>(content);
foreach (var table in result.Content.Tables)
{
Console.WriteLine(JsonConvert.SerializeObject(table));
}
Console.ReadLine();
}
Hope it helps :) 希望能帮助到你 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.