2017-12-21 25 views
0

私はAEMコンテンツAPIを使用していますが、JSON構造を提供していますが、リンクなどの特定のネストされたノードの場合は、ネストされたJSON文字列をJSONに変換する

サンプルペイロード - このペイロードに

{ 
    "Page Title": "Home", 
    "Page Description": "Sample Description", 
    "Key Words": "test1, test2, test 3", 
    "sections": [{ 
    "Lineup": { 
     "title": "Our Project Family", 
     "strategy": [{ 
     "title": "ASHJASH BASED", 
     "description": "This is a short description", 
     "links": ["{\"text\":\"income\",\"href\":\"/content/dam/usa/pdf/2017m_10.pdf\",\"desc\":\"This is a short description\"}", "{\"text\":\"Real Return\",\"href\":\"/content/dam/usa/pdf/singlepg.pdf\",\"desc\":\"This is a short description why to consider this\"}"], 
     "moreLink": "/content/us/home" 
     }, { 
     "title": "ALLOCATION", 
     "description": "This is a short description", 
     "links": ["{\"text\":\"fund\",\"href\":\"/content/dam/usa/pdf/2017m_10.pdf\",\"desc\":\"This is a short description\"}", "{\"text\":\"ETF\",\"href\":\"/content/dam/usa/pdf/2017m_10.pdf\",\"desc\":\"This is a short description\"}", "{\"text\":\"Active/Passive\",\"href\":\"/content/dam/usa/pdf/sat02017m_10.pdf\",\"desc\":\"This is a short description\"}"], 
     "moreLink": "/content/us/home" 
     }] 
    } 
    }] 
} 

、ネストされたリンク部は、JSON文字列です。

私のAPIでは、ペイロードを消費すると、フロントエンドに純粋なJSONオブジェクトを送信できるはずです。この構造はすべてのエンドポイントで異なるため、オブジェクト全体をJSONに変換する一般的な方法が必要です。

+0

はJSON.dump – zabusa

+2

'JSON.parse()を試してみました;' – pmaddi

+0

あなたのAPIからあなたのコードの一部を追加する場合、これは容易になるだろう – Stamos

答えて

0

コンテンツをJSONにリンクし、その結果をnode.jsサーバーからユーザーに応答します。サーバー:Hereが動作しています。

//ES6 
let res = { 
    "Page Title": "Home", 
    "Page Description": "Sample Description", 
    "Key Words": "test1, test2, test 3", 
    "sections": [{ 
    "Lineup": { 
     "title": "Our Project Family", 
     "strategy": [{ 
     "title": "ASHJASH BASED", 
     "description": "This is a short description", 
     "links": ["{\"text\":\"income\",\"href\":\"/content/dam/usa/pdf/2017m_10.pdf\",\"desc\":\"This is a short description\"}", "{\"text\":\"Real Return\",\"href\":\"/content/dam/usa/pdf/singlepg.pdf\",\"desc\":\"This is a short description why to consider this\"}"], 
     "moreLink": "/content/us/home" 
     }, { 
     "title": "ALLOCATION", 
     "description": "This is a short description", 
     "links": ["{\"text\":\"fund\",\"href\":\"/content/dam/usa/pdf/2017m_10.pdf\",\"desc\":\"This is a short description\"}", "{\"text\":\"ETF\",\"href\":\"/content/dam/usa/pdf/2017m_10.pdf\",\"desc\":\"This is a short description\"}", "{\"text\":\"Active/Passive\",\"href\":\"/content/dam/usa/pdf/sat02017m_10.pdf\",\"desc\":\"This is a short description\"}"], 
     "moreLink": "/content/us/home" 
     }] 
    } 
    }] 
} 

res.sections = res.sections.map(
    section => { 
    section.Lineup.strategy = section.Lineup.strategy.map(
     strategy => { 
     strategy.links = strategy.links.map(
      link => JSON.parse(link) 
     ) 

     return strategy; 
     } 
    ) 

    return section; 
    } 
) 
関連する問題