我有一个Json out put如下
[
{
"eng_trans": "wide, outstretched,"
},
{
"eng_trans": "width,breadth, town, street,earth, country, greatness."
},
{
"eng_trans": "wife, the mistress of the house,"
},
{
"eng_trans": "wide agricultural tract,"
},
{
"eng_trans": "waste land the land which is not suitabie for cultivation."
}]
我只需要带有out键的单词,需要在c#中形成一个String List。有没有更好的方法来做到这一点。
答案 0 :(得分:0)
public class RootObject
{
public string eng_trans { get; set; }
}
这将完成这项工作。
//list to store the words
List<string> wordList = new List<string>();
//Convert your json to string.I have already done it for you.
string json = "[{ \"eng_trans\": \"wide, outstretched,\" }, { \"eng_trans\": \"width,breadth, town, street,earth, country, greatness.\" }, { \"eng_trans\": \"wife, the mistress of the house,\" }, { \"eng_trans\": \"wide agricultural tract,\" }, { \"eng_trans\": \"waste land the land which is not suitabie for cultivation.\" }]";
//Add Newtonsoft.Json from Nuget to do this deserialization.
List<RootObject> eng_trans_List = Newtonsoft.Json.JsonConvert.DeserializeObject<List<RootObject>>(json);
foreach(RootObject obj in eng_trans_List)
{
string[] str = obj.eng_trans.Split(',');
foreach(string word in str)
{
wordList.Add(word);
}
}
答案 1 :(得分:0)
使用Newtonsoft.json.dll。
将Json字符串转换为数组。
检查此链接: https://www.newtonsoft.com/json https://www.codeproject.com/Questions/1074904/How-to-convert-Json-string-to-list-of-objects