2013-02-25 325 views
4

如何在JSON中使用if語句這裏是代碼: ................................... .................................................. ..如何使用JSON內部的if語句?

var config = 
      [ 
       { 
        "name"  : "SiteTitle", 
        "bgcolor" : "", 
        "color"  : "", 
        "position" : "TL", 
        "text"  : "step1", 
        "time"  : 5000 
       }, 
       { 
        "name"  : "Jawal", 
        "bgcolor" : "", 
        "color"  : "", 
        "text"  : "step2", 
        "position" : "BL", 
        "time"  : 5000 
       }, 
       { 
        "name"  : "Password", 
        "bgcolor" : "", 
        "color"  : "", 
        "text"  : "step3", 
        "position" : "TL", 
        "time"  : 5000 
       } 
      ], 

      //define if steps should change automatically 
      autoplay = false, 
      //timeout for the step 
      showtime, 
      //current step of the tour 
      step  = 0, 
      //total number of steps 
      total_steps = config.length; 

這是需要的結果是這樣的:

var config = 
     [ 

    if(page==true) {    
      { 
       "name"  : "SiteTitle", 
       "bgcolor" : "", 
       "color"  : "", 
       "position" : "TL", 
       "text"  : "step1", 
       "time"  : 5000 
      }, 
      { 
       "name"  : "Jawal", 
       "bgcolor" : "", 
       "color"  : "", 
       "text"  : "step2", 
       "position" : "BL", 
       "time"  : 5000 
      } 
    } else { 
      { 
       "name"  : "Password", 
       "bgcolor" : "", 
       "color"  : "", 
       "text"  : "step3", 
       "position" : "TL", 
       "time"  : 5000 
      } 
    } 
     ], 

      //define if steps should change automatically 
      autoplay = false, 
      //timeout for the step 
      showtime, 
      //current step of the tour 
      step  = 0, 
      //total number of steps 
      total_steps = config.length; 

其實這種方法是錯誤的,使得JavaScript語法錯誤。

回答

2

這是普通的JavaScript,而不是JSON。外移動if聲明:

if (page) { 
    var config = [ 
     { 
      "name"  : "SiteTitle", 
      "bgcolor" : "", 
      "color"  : "", 
      "position" : "TL", 
      "text"  : "step1", 
      "time"  : 5000 
     }, { 
      "name"  : "Jawal", 
      "bgcolor" : "", 
      "color"  : "", 
      "text"  : "step2", 
      "position" : "BL", 
      "time"  : 5000 
     } 
    ]; 
} else { 
    var config = [ 
     { 
      "name"  : "Password", 
      "bgcolor" : "", 
      "color"  : "", 
      "text"  : "step3", 
      "position" : "TL", 
      "time"  : 5000 
     } 
    ]; 
} 
+0

這將使一個語法錯誤,因爲你有互補碼 ,自動播放=假, 欣欣, step = 0, total_steps = config.length; – 2013-02-25 07:34:31

+0

@ MontaserEl-sawy:不,不是。嘗試一下。 – Blender 2013-02-25 07:35:57

+0

試過它的代碼將是: 如果(settingsPage){ \t \t \t \t變種配置= [ \t \t \t \t { \t \t \t \t \t 「名稱」: 「SiteTitle」, \t \t \t \t \t 「bgcolor」:「」, \t \t \t \t \t 「顏色」: 「」, \t \t \t \t \t 「位置」: 「TL」, \t \t \t \t \t 「文」: 「第一步」, \t \t \t \t \t 「時間」:5000 \t \t \t \t} \t \t \t \t \t \t \t \t]; \t \t \t}其他{ \t \t \t \t無功配置= [ \t \t \t \t { \t \t \t \t \t 「名」 「密碼」, \t \t \t \t \t 「的bgcolor」: 「」, \t \t \t \t \t「color」:「」, \t \t \t \t \t 「文」: 「第三步」, \t \t \t \t \t 「位置」: 「TL」, \t \t \t \t \t 「時間」:5000 \t \t \t \t} \t \t \t \t ]。 \t \t \t} \t \t \t \t, \t \t \t \t \t自動播放= FALSE, \t \t \t \t放映時間, \t \t \t \t \t步驟\t = 0, \t \t \t \t TOTAL_STEPS \t = config.length; 試試看,並告訴我會發生什麼? – 2013-02-25 07:44:31

0

或者是這樣的:

var config = 
    (page == true) ? 
    [    
     { 
      "name"  : "SiteTitle", 
      "bgcolor" : "", 
      "color"  : "", 
      "position" : "TL", 
      "text"  : "step1", 
      "time"  : 5000 
     }, 
     { 
      "name"  : "Jawal", 
      "bgcolor" : "", 
      "color"  : "", 
      "text"  : "step2", 
      "position" : "BL", 
      "time"  : 5000 
     } 
    : 
     { 
      "name"  : "Password", 
      "bgcolor" : "", 
      "color"  : "", 
      "text"  : "step3", 
      "position" : "TL", 
      "time"  : 5000 
     } 
    ]; 
+0

不,它不工作,因爲如果陳述後我必須把這樣的補充代碼: ,autoplay = false,showtime,step = 0,total_steps = config。長度; – 2013-02-25 07:46:48

+0

您可以在上面的代碼的最後一個括號的末尾添加一個逗號。 – 2013-02-25 07:59:18

0

你能做到這樣也(不是說這是最好的方式,但它的另一種方式,並可能在某些情況下非常有用)

let config = []; 
if (page) { 
    config.push({ 
     "name"  : "SiteTitle", 
     "bgcolor" : "", 
     "color"  : "", 
     "position" : "TL", 
     "text"  : "step1", 
     "time"  : 5000 
    }); 
    config.push({ 
     "name"  : "Jawal", 
     "bgcolor" : "", 
     "color"  : "", 
     "text"  : "step2", 
     "position" : "BL", 
     "time"  : 5000 
    }); 
} else { 
    config.push({ 
     "name"  : "Password", 
     "bgcolor" : "", 
     "color"  : "", 
     "text"  : "step3", 
     "position" : "TL", 
     "time"  : 5000 
    }); 
}