在JSON中需要帮助以某种方式检索数据

时间:2015-03-19 13:36:00

标签: javascript php jquery json

我的数据格式为:

{
  "name": "live",
  "description": " ",
  "aapUuId": "dsdfasds-dsdasa-asdasdsa",
  "backbutton": "10",
  "mainbutton": "10",
  "appInfo": {
    "items": [
      {
        "menuContent": "To continue now",
        "menuOrder": 1,
        "itemType": "static",
        "requestURL": "",
        "screen": {
          "items": [
            {
              "menuContent": "Yes",
              "menuOrder": 1,
              "itemType": "static",
              "requestURL": "",
              "screen": {
                "items": [
                  {
                    "menuContent": "Yes",
                    "menuOrder": 1,
                    "itemType": "static",
                    "requestURL": "",
                    "screen": {
                      "items": [
                        {
                          "menuContent": "Yes",
                          "menuOrder": 1,
                          "itemType": "static",
                          "requestURL": "",
                          "screen": {
                            "items": [
                              {
                                "menuContent": "Thank You",
                                "menuOrder": 0,
                                "itemType": "input",
                                "requestURL": "",
                                "screen": {

                                },
                                "link": ""
                              }
                            ],
                            "menuId": "1_1_1_1_1",
                            "menuHeader": "",
                            "menuFooter": ""
                          },
                          "link": ""
                        },
                        {
                          "menuContent": "No",
                          "menuOrder": 2,
                          "itemType": "static",
                          "requestURL": "",
                          "screen": {

                          },
                          "link": "1_1_1_1_1"
                        },
                        {
                          "menuContent": "Can't Say",
                          "menuOrder": 3,
                          "itemType": "static",
                          "requestURL": "",
                          "screen": {

                          },
                          "link": "1_1_1_1_1"
                        }
                      ],
                      "menuId": "1_1_1_1",
                      "menuHeader": "Are you satisfied with the condition of water supply in your area?",
                      "menuFooter": ""
                    },
                    "link": ""
                  },
                  {
                    "menuContent": "No",
                    "menuOrder": 2,
                    "itemType": "static",
                    "requestURL": "",
                    "screen": {

                    },
                    "link": "1_1_1_1"
                  },
                  {
                    "menuContent": "Can't Say",
                    "menuOrder": 3,
                    "itemType": "static",
                    "requestURL": "",
                    "screen": {

                    },
                    "link": "1_1_1_1"
                  }
                ],
                "menuId": "1_1_1",
                "menuHeader": "Are you satisfied with the condition of roads in your area?",
                "menuFooter": ""
              },
              "link": ""
            },
            {
              "menuContent": "No",
              "menuOrder": 2,
              "itemType": "static",
              "requestURL": "",
              "screen": {

              },
              "link": "1_1_1"
            },
            {
              "menuContent": "Can't Say",
              "menuOrder": 3,
              "itemType": "static",
              "requestURL": "",
              "screen": {

              },
              "link": "1_1_1"
            }
          ],
          "menuId": "1_1",
          "menuHeader": "Are you satisfied with the state of cleanliness in your area?",
          "menuFooter": ""
        },
        "link": ""
      },
      {
        "menuContent": "To participate later",
        "menuOrder": 2,
        "itemType": "static",
        "requestURL": "",
        "screen": {
          "items": [
            {
              "menuContent": "Today",
              "menuOrder": 1,
              "itemType": "static",
              "requestURL": "",
              "screen": {

              },
              "link": "1_1_1_1_1"
            },
            {
              "menuContent": "Tomorrow",
              "menuOrder": 2,
              "itemType": "static",
              "requestURL": "",
              "screen": {

              },
              "link": "1_1_1_1_1"
            },
            {
              "menuContent": "Day after Tomorrow",
              "menuOrder": 3,
              "itemType": "static",
              "requestURL": "",
              "screen": {

              },
              "link": "1_1_1_1_1"
            },
            {
              "menuContent": "During weekend",
              "menuOrder": 4,
              "itemType": "static",
              "requestURL": "",
              "screen": {

              },
              "link": "1_1_1_1_1"
            },
            {
              "menuContent": "Next Week",
              "menuOrder": 5,
              "itemType": "static",
              "requestURL": "",
              "screen": {

              },
              "link": "1_1_1_1_1"
            }
          ],
          "menuId": "1_2",
          "menuHeader": "Please confirm the suitable date when you wish to take the survey",
          "menuFooter": ""
        },
        "link": ""
      },
      {
        "menuContent": "Don't want to participate",
        "menuOrder": 3,
        "itemType": "static",
        "requestURL": "",
        "screen": {

        },
        "link": "1_1_1_1_1"
      }
    ],
    "menuId": "1",
    "menuHeader": "Welcome to DoIT Reality Check Survey!",
    "menuFooter": ""
  }
}

我需要以某种格式从中获取数据。

如果没有json ['link'],那么   只需打印菜单标题和项目

但如果提及链接1_1_1,则转到该菜单ID 1_1_1和 打印菜单标题和项目。

js或php任何方式。

我在php中试过这个。

function saving_for_app($finalAppData,$appid)
    {

        //Rest will be done tommorow follow dashboard/hitme
        $json = json_decode($finalAppData, true); // decode the JSON into an associative array

        $header = $json['appInfo']['menuHeader'];

        $data = array(
            'menu' => $header ,
            'app_id'=> $appid ,
            'master_id'=>0,
            'type'=>'Static',
            'menu_level'=>1,
            'menu_type'=>'0'
        );  ///// 1

        $this->db->insert('tbl_menu', $data);

        $insert_id = $this->db->insert_id();

        foreach ($json['appInfo']['items'] as $item) 
        {                    

              $data1 = array(
                    'menu' => $item['menuContent'],
                    'app_id'=> $appid ,
                    'master_id'=>$insert_id,
                    'type'=>'Static',
                    'menu_level'=>0,
                    'menu_type'=>'1'

              );
              $this->db->insert('tbl_menu', $data1);

              $insert_id1 = $this->db->insert_id();

              // 1 ke option 
              $linkstatus = $item['link'];

              //rolling link

              $header1 =  $item['screen']['menuHeader'];

              $data = array(
                    'menu' =>$header1,
                    'app_id'=> $appid,
                    'master_id'=>$insert_id1,
                    'type'=>'Static',
                    'menu_level'=>2,
                    'menu_type'=>'0'
               );

              $this->db->insert('tbl_menu', $data);
              $insert_id2 = $this->db->insert_id();

              foreach ($item['screen']['items'] as $item1) 
              {                      

                    $data1 = array(
                        'menu' => $item1['menuContent'],
                        'app_id'=> $appid ,
                        'master_id'=>$insert_id2,
                        'type'=>'Static',
                        'menu_level'=>0,
                        'menu_type'=>'1'
                    );
                    $this->db->insert('tbl_menu', $data1);

                    $linkstatus = $item1['link'];

                    //rolling link

                    $insert_idx1 = $this->db->insert_id();                        

                    $header1x =  $item1['screen']['menuHeader'];

                    $data = array(
                          'menu' =>$header1x,
                          'app_id'=> $appid,
                          'master_id'=>$insert_idx1,
                          'type'=>'Static',
                          'menu_level'=>3,
                          'menu_type'=>'0'
                     );

                    $this->db->insert('tbl_menu', $data);
                    $insert_id2x = $this->db->insert_id();

                    foreach ($item1['screen']['items'] as $item2) 
                    {


                          $data1 = array(
                              'menu' => $item2['menuContent'],
                              'app_id'=> $appid ,
                              'master_id'=>$insert_id2x,
                              'type'=>'Static',
                              'menu_level'=>0,
                              'menu_type'=>'1'
                          );
                          $this->db->insert('tbl_menu', $data1);

                          $insert_idx2 = $this->db->insert_id();

                          $linkstatus = $item2['link'];

                          //rolling link


                          $header2x =  $item2['screen']['menuHeader'];

                          $data = array(
                                'menu' =>$header2x,
                                'app_id'=> $appid,
                                'master_id'=>$insert_idx2,
                                'type'=>'Static',
                                'menu_level'=>4,
                                'menu_type'=>'0'
                           );

                          $this->db->insert('tbl_menu', $data);
                          $insert_id3 = $this->db->insert_id();


                          foreach ($item2['screen']['items'] as $item3) 
                          {


                                  $data1 = array(
                                      'menu' => $item3['menuContent'],
                                      'app_id'=> $appid ,
                                      'master_id'=>$insert_id3,
                                      'type'=>'Static',
                                      'menu_level'=>0,
                                      'menu_type'=>'1'
                                  );
                                  $this->db->insert('tbl_menu', $data1);

                                  $insert_idx3 = $this->db->insert_id();

                                  $linkstatus = $item3['link'];

                                  //rolling link

                                  $header3x =  $item3['screen']['menuHeader'];

                                  $data = array(
                                        'menu' =>$header3x,
                                        'app_id'=> $appid,
                                        'master_id'=>$insert_idx3,
                                        'type'=>'Static',
                                        'menu_level'=>5,
                                        'menu_type'=>'0'
                                   );

                                  $this->db->insert('tbl_menu', $data);
                                  $insert_id5 = $this->db->insert_id();


                                    foreach ($item3['screen']['items'] as $item4) 
                                    {


                                          $data1 = array(
                                              'menu' => $item4['menuContent'],
                                              'app_id'=> $appid ,
                                              'master_id'=>$insert_id5,
                                              'type'=>'Static',
                                              'menu_level'=>0,
                                              'menu_type'=>'1'
                                          );
                                          $this->db->insert('tbl_menu', $data1);

                                          $insert_idx4 = $this->db->insert_id();


                                          $linkstatus = $item4['link'];

                                          //rolling link                                             

                                          $header4x =  $item4['screen']['menuHeader'];

                                          $data = array(
                                                'menu' =>$header4x,
                                                'app_id'=> $appid,
                                                'master_id'=>$insert_idx4,
                                                'type'=>'Static',
                                                'menu_level'=>6,
                                                'menu_type'=>'0'
                                           );

                                          $this->db->insert('tbl_menu', $data);
                                          $insert_id6 = $this->db->insert_id();




                                    }



                            }



                    }





              }


        }


    }

但是这样我缺少链接ID。和多个foreach循环相当令人困惑。请帮助。任何建议或建议这样做。

0 个答案:

没有答案