我有以下JSon字符串
{
'id': '346582587961401930',
'form_id': '62455437457962',
'ip': '79.123.81.69',
'created_at': '2016-08-09 16:09:47',
'status': 'ACTIVE',
'new': '0',
'flag': '0',
'updated_at': '2016-08-09 16:10:49',
'answers': {
'4': {
'text': '<p><span style=\'font-size:14pt;\'><strong>Ethical Leaders. Global Changemakers</strong></span></p>',
'type': 'control_text'
},
'5': {
'text': ' ',
'type': 'control_textbox',
'answer': 'John'
},
'6': {
'type': 'control_textbox',
'text': 'Middle'
},
'7': {
'text': ' ',
'type': 'control_textbox',
'answer': 'Smith'
},
'8': {
'text': 'Address',
'type': 'control_address'
},
'9': {
'text': 'E-mail',
'type': 'control_email',
'answer': 'JSmith@gmail.com'
},
'10': {
'text': 'Cell phone',
'type': 'control_phone',
'answer': {
'area': '213',
'phone': '3923095'
},
'prettyFormat': '(213) 3923095'
},
'11': {
'type': 'control_textbox',
'text': 'Anticipated Start Semester / Year',
'answer': '2018 Fall'
},
'13': {
'type': 'control_textbox',
'text': 'Current high school / College',
'answer': 'Wood Ranch'
},
'14': {
'type': 'control_textbox',
'text': 'Event name',
'answer': 'Testing'
},
'15': {
'type': 'control_textarea',
'text': 'Notes',
'answer': 'This is a test'
},
'16': {
'text': 'Home phone',
'type': 'control_phone'
},
'12': {
'answer': 'Biology and Bioethics (Pre-Med)\r\nUnknown'
}
}
}
带有&#39;数字&#39;的元素表示问题ID,如&#39; 4&#39;。
在C#中(使用Newtonsoft.Json;使用Newtonsoft.Json.Linq;)我正在寻找一种基于id和每个get属性值搜索和提取特定问题的方法。如果属性也是JSon我也希望能够得到它们。
例如:
我想得到&#39;回答&#39;问题ID的属性&#39; 5&#39;和&#39;区域&#39;和&#39;电话&#39;在&#39;回答&#39;问题ID&#39; 10&#39; 我尝试了不同的方法但找不到一致的方法来获取它
谢谢
myalo
答案 0 :(得分:0)
这是我到目前为止,它通过使用动态两次对我有用。没有迭代的代码看起来更干净
dynamic data = JObject.Parse(sub);
Console.WriteLine("id:" + data.id.ToString());
Console.WriteLine("ip:" + data.ip.ToString());
string answersData = data.answers.ToString();
//JObject answers = JObject.Parse(answersData);
dynamic danswers = JObject.Parse(answersData);
//get each answer - here we will use the mapping
//to-do: replace Console with saving to data store
Console.WriteLine("First name= " + (string)danswers["5"]["answer"]);
Console.WriteLine("Middle Name= " + getAnswer(danswers, "6", "control_textbox"));
Console.WriteLine("Last Name=" + getAnswer(danswers,"7","control_textbox"));
Console.WriteLine("Address=" + getAnswer(danswers, "8", "control_textbox"));
Console.WriteLine("Email=" + getAnswer(danswers, "9", "control_textbox"));
Console.WriteLine("Phone=" + getAnswer(danswers, "10", "control_textbox"));
Console.WriteLine("Anticipated Start Semester=" + getAnswer(danswers, "11", "control_textbox"));
Console.WriteLine("Current high school / College=" + getAnswer(danswers, "13", "control_textbox"));
Console.WriteLine("Event name=" + getAnswer(danswers, "14", "control_textbox"));
Console.WriteLine("Notes=" + getAnswer(danswers, "15", "control_textbox"));
Console.ReadLine();
getAnswer
private static string getAnswer(JObject danswers,string qid, string answertype)
{
// more "type" later
string returnValue = "";
if (danswers[qid] != null && ((string)danswers[qid]["type"] == "control_textbox" || (string)danswers[qid]["type"] == "control_email" || (string)danswers[qid]["type"] == "control_textarea"))
{
returnValue = (string)danswers[qid]["answer"];
}
else
{
//need better error
returnValue = "Not found or type is not handled";
}
return (returnValue);
}