2016-11-07 73 views
0

我有一個像下面動態ID和特殊字符

{ 
    "!type": "alarm", 
    "$": { 
    "12279": { 
     "!type": "alarm", 
     "title": "Default", 
     "$": { 
     "5955": { 
      "!type": "alarm", 
      "name": "Wake", 
      "day": "SUN", 
      "startTime": "06:00" 
     }, 
     "29323": { 
      "!type": "alarm", 
      "name": "Away", 
      "day": "SUN", 
      "startTime": "08:00" 
     }, 
     "2238": { 
      "!type": "alarm", 
      "name": "Home", 
      "day": "SUN", 
      "startTime": "18:00" 
     } 
     } 
    } 
    } 
} 

我FBS看起來像這樣

namespace space.alarm; 
 

 
table Atom{ 
 
    !type:string; 
 
    name:string; 
 
    startDay:string; 
 
    startTime:string; } 
 

 
table AtomShell{ 
 
    key:string (required, key); 
 
    value: Atom; } 
 

 
table Alarm{ 
 
    !type:string; 
 
    title:string; 
 
    $:[AtomShell]; } 
 

 

 
table AlarmShell{ 
 
    key:string (required, key); 
 
    value:Alarm; } 
 

 

 
table Weeklyalarm{ 
 
    !type:string; 
 
    $:[AlarmShell]; } root_type Weeklyalarm;

我試着去實現谷歌JSON數據平緩衝區,但我得到錯誤,如

  1. alarm.fbs:4:0:錯誤:非法字符:!
  2. alarm.fbs:23:0:error:illegal character:$(我已經刪除!從 !類型並將$更改爲美元來測試平緩衝區的工作 但我無法更改動態ID)
  3. Sample.json:25:0:錯誤:未知的領域:12279

現在我的問題,

  1. 是否有可能在平板緩衝區使用動態的ID,如果可能的話如何0​​應我繼續?
  2. 可以在ID中使用特殊字符,如果可能的話該怎麼做?

在此先感謝。

回答

0

字段名稱中不能有像!$這樣的字符。只需使用type而不是!type等。

不確定動態ID是什麼意思。所有的字段名稱(鍵)都必須在模式中聲明,所以它們不能是動態的。您仍然可以實現類似的結果不過,如果你讓你的JSON是這個樣子:

{ 
    "type": "alarm", 
    "data": [ 
    { 
     id: "12279", 
     "type": "alarm", 
     "title": "Default", 
     "data": [ 
     { 
      "id": "5955", 
      "type": "alarm", 
      "name": "Wake", 
      "day": "SUN", 
      "startTime": "06:00" 
     }, 
     { 
      "id": "29323", 
      "type": "alarm", 
      "name": "Away", 
      "day": "SUN", 
      "startTime": "08:00" 
     }, 
     { 
      "id": "2238", 
      "type": "alarm", 
      "name": "Home", 
      "day": "SUN", 
      "startTime": "18:00" 
     } 
     ] 
    } 
    ] 
} 

然後做出相應的模式。

請注意,我將「動態」列表放入一個向量中,並將id移動到對象本身中。

其他技巧:非動態字符串值(如"alarm")將佔用更少的空間,如果您將它們改爲枚舉。