2013-11-22 28 views
0

我有一個JSON.NET令牌像這樣:JSON令牌保持返回NULL

{ 
    "version": "1.0", 
    "createdBy": "PowerStats v1.0", 
    "test": "ohoh", 
    "DSNumber": { 
    "value": "82" 
    }, 
    "title": { 
    "value": "" 
    }, 
    "footnote": { 
    "value": "" 
    }, 
    "flagRSE": { 
    "value": "30,50", 
    "symbol": "!,!!" 
    }, 
    "weight": { 
    "type": "0", 
    "varName": "WTA000", 
    "label": "weight_var" 
    }, 
    "filters": { 
    "filter_1": { 
     "component": { 
     "varName": "JOBEARN2", 
     "filterType": "Range", 
     "format": "Cont", 
     "label": "Job: Earnings from work while enrolled (including work-study)", 
     "element": { 
      "startValue": "1", 
      "endValue": "", 
      "label": "X >= 1" 
     } 
     } 
    }, 
    "filter_2": { 
     "component": { 
     "varName": "JOBROLE2", 
     "filterType": "Dist", 
     "format": "Disc", 
     "label": "Job: Primary role as student or employee (including work-study)", 
     "element": { 
      "value": "1", 
      "label": "A student working to meet expenses" 

     } 
    } 
    }, 
    "columns": { 
    "component": { 
     "tag": "CAT", 
     "missingValue": "4,5,6,7,8,9,10,13,14,15,16,17,18,19,20,21,22,23,-3", 
     "format": "Disc", 
     "varName": "MAJORS23", 
     "label": "Field of study: undergraduate (23 categories)", 
     "element": [ 
     { 
      "value": "0", 
      "label": "Undecided" 
     }, 
     { 
      "value": "1", 
      "label": "Computer and information sciences" 
     }, 
     { 
      "value": "2", 
      "label": "Engineering and engineering technology" 
     }, 
     { 
      "value": "3", 
      "label": "Biological and physical science, science tech" 
     }, 
     { 
      "value": "11", 
      "label": "Personal and consumer services" 
     }, 
     { 
      "value": "12", 
      "label": "Manufacturing,construction,repair & transportation" 
     } 
     ] 
    } 
    }, 
    "rows": { 
    "component": [ 
     { 
     "tag": "CUT", 
     "missingValue": "", 
     "format": "Cont", 
     "varName": "GPA", 
     "label": "Grade point average", 
     "element": [ 
      { 
      "startValue": "1", 
      "endValue": "249", 
      "label": "Lower than 2.50" 
      }, 
      { 
      "startValue": "250", 
      "endValue": "299", 
      "label": "2.50 - 2.99" 
      }, 
      { 
      "startValue": "300", 
      "endValue": "349", 
      "label": "3.00 - 3.49" 
      }, 
      { 
      "startValue": "350", 
      "endValue": "400", 
      "label": "3.50 or higher" 
      } 
     ] 
     }, 
     { 
     "tag": "CAT", 
     "missingValue": "", 
     "format": "Disc", 
     "varName": "STEMMAJ", 
     "label": "Major field of study with a focus on STEM fields", 
     "element": [ 
      { 
      "value": "1", 
      "label": "Math/Computer/Sciences/Engineering/Technologies" 
      }, 
      { 
      "value": "2", 
      "label": "Social/behavioral sciences" 
      }, 
      { 
      "value": "4", 
      "label": "Non-STEM field" 
      }, 
      { 
      "value": "5", 
      "label": "Undecided or not in a degree program" 
      } 
     ] 
     } 
    ] 
    } 
} 

但是當我這樣做:

// This is correct and returns 1. 
int columnsCount = token["columns"].Count(); 

// This errors out with the error below 
int componentCount = token["columns.component"].Count(); 

的錯誤是:

"Value cannot be null. Parameter name: source" 

但爲什麼?

+1

令牌[「列」] [「組件」] –

回答

2

token["columns"]本身就是另一個JSON對象。你需要引用裏面的東西也以同樣的方式,即

token["columns"]["component"] 
+0

咄我做了一件愚蠢的...你是正確的absoultely。 – cdub