# Json (newsoft) -获取特定的元素

本文关键字:元素 获取 Json newsoft | 更新日期: 2023-09-27 18:04:43

我有以下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'
                    }
                 }
              }

带有'number'的元素表示问题id,如'4'。

在c#中(使用Newtonsoft.Json;使用Newtonsoft.Json.Linq;)我正在寻找一种方法来搜索和提取基于id和每个获得属性值的特定问题。如果属性也是JSon,我也希望能够得到它们。

例如:

我想获得问题id为'5'的'answer'属性的值以及问题id为'10'的'answer'下的'area'和'phone'我尝试了不同的方法,但无法找到一个一致的方法来获得它

感谢

myalo

# Json (newsoft) -获取特定的元素

这就是我目前所拥有的,它通过使用两次动态来为我工作。没有了

这个迭代,代码看起来更干净。
            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"]["answerswer"]);
            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]["answerswer"];
      }
      else
      {
         //need better error
          returnValue = "Not found or type is not handled";
      }
      return (returnValue);
  }