2017-08-08 93 views
-4

我想在我的代碼中解析嵌套的json,但是我在json建模中遇到了困難,請幫我解決下面的json問題。Angular2:嵌套JSON的模型

{ "label": { 
"Application": { 
    "_default": { 
    "defaultIcon": "", 
    "defaultRank": 1, 
    "defaultLabel": "Name", 
    "defaultColor": "#ffffff", 
    "displayName": "Application" 
    } 
}} 
+0

將您的代碼添加到您想要解析的地方 – Faisal

+0

label [應用] ._ default [displayName] 使用上面的代碼但m得到錯誤,_default未定義 – Kunal

+3

您的問題是不可理解的。你想要「塑造」什麼? 「解析」是什麼意思?你需要幫助做什麼?這與Angular或Angular模板有什麼關係? – 2017-08-08 09:39:24

回答

0

如果您想要對其內容進行完整輸入,則必須爲每個級別的JSON導出一個接口。

export interface DefaultApplication { 
    defaultIcon: string; 
    defaultRank: string; 
    defaultLabel: string; 
    defaultColor: string; 
    displayName: string;  
} 

export interface Application { 
    _default: DefaultApplication; 
} 

export interface Label { 
    Application: Application; 
} 

編輯:你應該分析使用JSON.parse的JSON使用它作爲一個對象之前,否則這將是一個string

+1

是什麼讓你認爲這是OP的問題的答案?其實,你認爲OP的問題**是**? – 2017-08-08 09:40:07

+0

對我來說,這個問題可以重新格式化爲「如何正確輸入嵌套數據我的JSON」。 – Supamiu

0

沒有解析的機會,因爲您有4個字符,只有3個個字符。 您的JSON應該看起來像:

{ 
    "label": { 
     "Application": { 
      "_default": { 
       "defaultIcon": "", 
       "defaultRank": 1, 
       "defaultLabel": "Name", 
       "defaultColor": "#ffffff", 
       "displayName": "Application" 
      } 
     } 
    } 
} 

下一次,你應該使用一些JSON格式器/驗證f.e this,而不是寫新的問題。