解析JSON字符串不一致字段名 [英] Parse JSON string with inconsistent field names
问题描述
我有在反序列化下面的JSON结构的麻烦。每个节点包含一个ID和多语言代码值。语言属性的数目并不一致。但我需要这些值作为具有语言字段和值字段对象列表。
[{
ID:w_312457,
工程:深发油炸,
蒙古包:Frittieren
},
{
ID:w_312458,
FRE:炒,
蒙古包:braten(以OL)
},
{
ID:w_312477,
蒙古包:PERFEKTgewürzteUND abgestimmteSoße
}]
我试着用 JsonPropertyName
属性和我的ID值。但是,对于郎节点,我不知道是什么名字我可以指定。以下是我的CLR对象,
公共类字
{
公共字符串ID {搞定;组; } //这是工作
//我可以在这里指定。我需要的对象的列表中的每个具有朗代码和值。
}
方法1:
一种方法是简单地将所有的价值观和检查,如果存在的话。例如,这是包含所有语言值类:
公共类字
{
公字符串ID {搞定;组; }
公共字符串工程{搞定;组; }
公共字符串蒙古包{搞定;组; }
公共字符串FRE {搞定;组; }
}
和你得到的名单,如:
VAR字= JsonConvert.DeserializeObject<名单,LT; Word和GT;>(JSON);
当然,这是假设只有3种语言,更不会被添加(这不会发生!)
方法2:
如图的this 线程,你可以反序列化到一个列表清单<字典<字符串,字符串>>>(
VAR字= JsonConvert.DeserializeObject<:字典对象是这样的JSON);
和您可以访问所有的键和值是这样的:
的foreach(字VAR字)
{
的foreach(在word.Keys VAR键)
{
控制台。的WriteLine($为重点{}键值是{字[关键]});
}
}
这将产生以下结果:
密钥ID
值为w_312457
为重点工程的价值是深发油炸为重点蒙古包
值为Frittieren
为密钥ID
值为w_312458
$ b为重点FRE
$ b值油炸
价值的关键蒙古包是braten(以OL)
密钥ID
值是关键蒙古包w_312477
值PERFEKTgewürzteUND abgestimmteSoße
I am having trouble in deserializing the following JSON structure. Each node contains a ID and multiple language code with values. The number of language attributes is not consistent. But I need those values as a list of objects which has a Language field and a Value field.
[{
"id":"w_312457",
"eng":"deep-fat frying",
"ger":"Frittieren"
},
{
"id":"w_312458",
"fre":"frying",
"ger":"braten (in Öl)"
},
{
"id":"w_312477",
"ger":"perfekt gewürzte und abgestimmte Soße "
}]
I tried using JsonPropertyName
attribute and I got the ID value. But for lang nodes, I dont know what name I can specify. Following is my CLR object,
public class Word
{
public string Id { get; set; } // This is working
// What can I specify here. I need a list of objects each with a lang code and value.
}
Method 1:
One approach would be to simply adding all the values and checking if they exist. For example this is the class that contains all the language values:
public class Word
{
public string id { get; set; }
public string eng { get; set; }
public string ger { get; set; }
public string fre { get; set; }
}
And you get the list such as:
var words = JsonConvert.DeserializeObject<List<Word>>(json);
Of course this assumes there are only 3 languages and more will not be added (which never happens!)
Method 2:
As shown in this thread, you can deserialize to a list of dictionary objects like this:
var words = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(json);
And you can access all keys and values like this:
foreach (var word in words)
{
foreach (var key in word.Keys)
{
Console.WriteLine($"value for the key {key} is {word[key]}");
}
}
This will produce the following result:
value for the key id is w_312457
value for the key eng is deep-fat frying
value for the key ger is Frittieren
value for the key id is w_312458
value for the key fre is frying
value for the key ger is braten (in Öl)
value for the key id is w_312477
value for the key ger is perfekt gewürzte und abgestimmte Soße
这篇关于解析JSON字符串不一致字段名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!