2013-04-15 22 views
0

我正在嘗試使用預製庫來配置SIP電話。他們似乎使用JSON作爲模板,並且我處於陌生領域。有許多品牌/型號,有些擁有多個模板。我希望能夠獲取模板的內容並將其轉換爲一個PHP表單。我不確定如何編寫不同的部分名稱。我想編寫一個PHP函數,無論使用的模板文件如何,都可以完成。 這裏是我一起工作的模板2:將第三方JSON文件解釋爲PHP表格

{ 
    "template_data":{ 
    "category":[ 
     { 
     "name":"keys", 
     "subcategory":[ 
      { 
      "name":"Soft Keys (9480i\/6755i\/6757i)", 
      "item":[ 
       { 
       "description":"Soft Keys (9480i\/6755i\/6757i)", 
       "type":"loop", 
       "loop_start":"1", 
       "loop_end":"18", 
       "data":{ 
        "item":[ 
        { 
         "variable":"$softkey_type", 
         "default_value":"empty", 
         "category":"keys", 
         "description":"Soft Key {$count} Type", 
         "type":"list", 
         "data":[ 
         { 
          "text":"Empty", 
          "value":"empty" 
         }, 
         { 
          "text":"Services", 
          "value":"services" 
         }, 
         { 
          "text":"Speed Dial", 
          "value":"speeddial" 
         }, 
         { 
          "text":"Line", 
          "value":"line" 
         }, 
         { 
          "text":"BLF", 
          "value":"blf" 
         }, 
         { 
          "text":"List", 
          "value":"list" 
         }, 
         { 
          "text":"Do Not Disturb", 
          "value":"dnd" 
         }, 
         { 
          "text":"XML URL", 
          "value":"xml" 
         }, 
         { 
          "text":"BLF Transfer", 
          "value":"blfxfer" 
         }, 
         { 
          "text":"Intercom", 
          "value":"icom" 
         }, 
         { 
          "text":"Pickup", 
          "value":"pickup" 
         }, 
         { 
          "text":"Flash", 
          "value":"flash" 
         }, 
         { 
          "text":"Special Prefix Code", 
          "value":"spre" 
         }, 
         { 
          "text":"Park", 
          "value":"park" 
         }, 
         { 
          "text":"Directory", 
          "value":"directory" 
         }, 
         { 
          "text":"Callers", 
          "value":"callers" 
         } 
         ] 
        }, 
        { 
         "variable":"$softkey_label", 
         "default_value":"", 
         "description":"Soft Key {$count} Label", 
         "type":"input" 
        }, 
        { 
         "variable":"$softkey_value", 
         "default_value":"", 
         "description":"Soft Key {$count} Value", 
         "type":"input" 
        }, 
        { 
         "variable":"$softkey_states", 
         "default_value":"Any", 
         "description":"Soft Key {$count} States", 
         "type":"list", 
         "data":[ 
         { 
          "text":"Any", 
          "value":"connected,idle,incoming,outgoing" 
         }, 
         { 
          "text":"Connected", 
          "value":"connected" 
         }, 
         { 
          "text":"Idle", 
          "value":"idle" 
         }, 
         { 
          "text":"Incoming", 
          "value":"incoming" 
         }, 
         { 
          "text":"Outgoing", 
          "value":"outgoing" 
         }, 
         { 
          "text":"Idle or Incoming", 
          "value":"idle,incoming" 
         }, 
         { 
          "text":"Connected or Outgoing", 
          "value":"connected,outgoing" 
         }, 
         { 
          "text":"None (Hide Button)", 
          "value":[ 

          ] 
         } 
         ] 
        }, 
        { 
         "type":"break" 
        } 
        ] 
       } 
       } 
      ] 
      } 
     ] 
     } 
    ] 
    } 
} 

,這裏是另一個:

{ 
    "template_data":{ 
    "category":[ 
     { 
     "name":"keys", 
     "subcategory":[ 
      { 
      "name":"Multi Function Keys", 
      "item":[ 
       { 
       "variable":"$key_mode_01", 
       "default_value":"", 
       "description":"Key 1 Mode", 
       "type":"list", 
       "data":[ 
        { 
        "text":"Speed Dial", 
        "value":"0" 
        }, 
        { 
        "text":"BLF", 
        "value":"1" 
        }, 
        { 
        "text":"Presence Watcher", 
        "value":"2" 
        }, 
        { 
        "text":"Eventlist BLF", 
        "value":"3" 
        } 
       ] 
       }, 
       { 
       "variable":"$key_name_01", 
       "default_value":"Speed Dial 1", 
       "description":"Key Name", 
       "type":"input" 
       }, 
       { 
       "variable":"$speed_dial_01", 
       "default_value":"*01", 
       "description":"Value #1", 
       "type":"input" 
       }, 
       { 
       "type":"break" 
       }, 
       { 
       "variable":"$key_mode_02", 
       "default_value":"", 
       "description":"Key 2 Mode", 
       "type":"list", 
       "data":[ 
        { 
        "text":"Speed Dial", 
        "value":"0" 
        }, 
        { 
        "text":"BLF", 
        "value":"1" 
        }, 
        { 
        "text":"Presence Watcher", 
        "value":"2" 
        }, 
        { 
        "text":"Eventlist BLF", 
        "value":"3" 
        } 
       ] 
       }, 
       { 
       "variable":"$key_name_02", 
       "default_value":"Speed Dial 2", 
       "description":"Key Name", 
       "type":"input" 
       }, 
       { 
       "variable":"$speed_dial_02", 
       "default_value":"*02", 
       "description":"Value #2", 
       "type":"input" 
       }, 
       { 
       "type":"break" 
       }, 
       { 
       "variable":"$key_mode_03", 
       "default_value":"", 
       "description":"Key 3 Mode", 
       "type":"list", 
       "data":[ 
        { 
        "text":"Speed Dial", 
        "value":"0" 
        }, 
        { 
        "text":"BLF", 
        "value":"1" 
        }, 
        { 
        "text":"Presence Watcher", 
        "value":"2" 
        }, 
        { 
        "text":"Eventlist BLF", 
        "value":"3" 
        } 
       ] 
       }, 
       { 
       "variable":"$key_name_03", 
       "default_value":"Speed Dial 3", 
       "description":"Key Name", 
       "type":"input" 
       }, 
       { 
       "variable":"$speed_dial_03", 
       "default_value":"*03", 
       "description":"Value #3", 
       "type":"input" 
       }, 
       { 
       "type":"break" 
       }, 
       { 
       "variable":"$key_mode_04", 
       "default_value":"", 
       "description":"Key 4 Mode", 
       "type":"list", 
       "data":[ 
        { 
        "text":"Speed Dial", 
        "value":"0" 
        }, 
        { 
        "text":"BLF", 
        "value":"1" 
        }, 
        { 
        "text":"Presence Watcher", 
        "value":"2" 
        }, 
        { 
        "text":"Eventlist BLF", 
        "value":"3" 
        } 
       ] 
       }, 
       { 
       "variable":"$key_name_04", 
       "default_value":"Speed Dial 4", 
       "description":"Key Name", 
       "type":"input" 
       }, 
       { 
       "variable":"$speed_dial_04", 
       "default_value":"*04", 
       "description":"Value #4", 
       "type":"input" 
       }, 
       { 
       "type":"break" 
       }, 
       { 
       "variable":"$key_mode_05", 
       "default_value":"", 
       "description":"Key 5 Mode", 
       "type":"list", 
       "data":[ 
        { 
        "text":"Speed Dial", 
        "value":"0" 
        }, 
        { 
        "text":"BLF", 
        "value":"1" 
        }, 
        { 
        "text":"Presence Watcher", 
        "value":"2" 
        }, 
        { 
        "text":"Eventlist BLF", 
        "value":"3" 
        } 
       ] 
       }, 
       { 
       "variable":"$key_name_05", 
       "default_value":"Speed Dial 5", 
       "description":"Key Name", 
       "type":"input" 
       }, 
       { 
       "variable":"$speed_dial_05", 
       "default_value":"*05", 
       "description":"Value #5", 
       "type":"input" 
       }, 
       { 
       "type":"break" 
       }, 
       { 
       "variable":"$key_mode_06", 
       "default_value":"", 
       "description":"Key 6 Mode", 
       "type":"list", 
       "data":[ 
        { 
        "text":"Speed Dial", 
        "value":"0" 
        }, 
        { 
        "text":"BLF", 
        "value":"1" 
        }, 
        { 
        "text":"Presence Watcher", 
        "value":"2" 
        }, 
        { 
        "text":"Eventlist BLF", 
        "value":"3" 
        } 
       ] 
       }, 
       { 
       "variable":"$key_name_06", 
       "default_value":"Speed Dial 6", 
       "description":"Key Name", 
       "type":"input" 
       }, 
       { 
       "variable":"$speed_dial_06", 
       "default_value":"*06", 
       "description":"Value #6", 
       "type":"input" 
       }, 
       { 
       "type":"break" 
       }, 
       { 
       "variable":"$key_mode_07", 
       "default_value":"", 
       "description":"Key 7 Mode", 
       "type":"list", 
       "data":[ 
        { 
        "text":"Speed Dial", 
        "value":"0" 
        }, 
        { 
        "text":"BLF", 
        "value":"1" 
        }, 
        { 
        "text":"Presence Watcher", 
        "value":"2" 
        }, 
        { 
        "text":"Eventlist BLF", 
        "value":"3" 
        } 
       ] 
       }, 
       { 
       "variable":"$key_name_07", 
       "default_value":"Speed Dial 7", 
       "description":"Key Name", 
       "type":"input" 
       }, 
       { 
       "variable":"$speed_dial_07", 
       "default_value":"*07", 
       "description":"Value #7", 
       "type":"input" 
       } 
      ] 
      } 
     ] 
     } 
    ] 
    } 
} 

它看起來像這樣的目的是做正是我想要的,我只是不知道如何開始。我能夠從模板文件中檢索數據,但是從那裏丟失了。我已經嘗試使用json_decode,但輸出似乎沒有任何友好的工作。我也嘗試使用PHP來獲取模板的內容,然後使用JavaScript來拆除它。這是有效的,但是需要每個文件更多的靜態變量,並且如果文件的提供者改變了某些內容,則可能需要更多的時間編輯。如果有人知道最簡單的方法來解決這個問題,將不勝感激。

回答

0

我會使用handlebars.js來顯示信息。看看這裏:

http://handlebarsjs.com/ 

我每天都用它它的那樣簡單:

<tr> 
<td>{{clock_number}}</td> 
<td style="width: 150px;">{{employee_name}}</td> 
<td>{{hourly_wage}}</td> 
<td><a href='employee.php?clock_number={{clock_number}}'>Edit</a></td> 
</tr> 

真的不錯的免費課程,以幫助您打算:

https://tutsplus.com/course/30-days-to-learn-jquery/ 
+0

我試圖避免靜態編碼儘可能爲每個可能的變量名稱。在完美的世界中,我只需要查找某些變量設置(廣播,列表,輸入等),然後根據該值顯示適當的表單輸入類型,但我不知道文件中的密鑰是什麼歸檔。爲了讓事情更有趣,我不知道這些值甚至是一致的。 – PHPBandJ

+0

嗯狡猾,可能是我建議你已經想到的任何東西。 – tattooedgeek