2017-03-26 12 views
-2

JSON.parse()関数でこの後続の文字列を解析できません。 最初は最初と最後の二重引用符を削除しようとしました。そして、私はエスケープ文字を削除しました。jsonオブジェクトに含まれる文字列をエスケープ文字で解析する方法は?

このタイプのJSON応答を解析するにはどうすればよいですか?

{ 
    "timestamp": 1490545425158, 
    "reports": { 
    "statusCode": 200, 
    "body": "{\"responseCode\":\"200\",\"responseMessage\":\"Success\",\"getevent\":[{\"eventID\":\"24844563\",\"channelId\":21,\"channelStbNumber\":\"861\",\"channelHD\":\"false\",\"channelTitle\":\"Gold\",\"epgEventImage\":null,\"certification\":\"U\",\"displayDateTimeUtc\":\"2017-03-25 16:00:00.0\",\"displayDateTime\":\"2017-03-26 00:00:00.0\",\"displayDuration\":\"06:00:00\",\"siTrafficKey\":\"1:2169:30291203\",\"programmeTitle\":\"Great Golden Oldies\",\"programmeId\":\"GBSSJ\",\"episodeId\":\"\",\"shortSynopsis\":\"Mixture of '50s to '70s songs.\",\"longSynopsis\":null,\"actors\":\"\",\"directors\":\"\",\"producers\":\"\",\"genre\":\"Music & Dance\",\"subGenre\":\"General\",\"live\":false,\"premier\":false,\"ottBlackout\":false,\"highlight\":null,\"contentId\":null,\"contentImage\":null,\"groupKey\":null,\"vernacularData\":[]},{\"eventID\":\"24844564\",\"channelId\":21,\"channelStbNumber\":\"861\",\"channelHD\":\"false\",\"channelTitle\":\"Gold\",\"epgEventImage\":null,\"certification\":\"U\",\"displayDateTimeUtc\":\"2017-03-25 22:00:00.0\",\"displayDateTime\":\"2017-03-26 06:00:00.0\",\"displayDuration\":\"04:00:00\",\"siTrafficKey\":\"1:2169:30291204\",\"programmeTitle\":\"Breakfast Time\",\"programmeId\":\"GBSSK\",\"episodeId\":\"\",\"shortSynopsis\":\"Wake up to Golden Oldies.\",\"longSynopsis\":null,\"actors\":\"\",\"directors\":\"\",\"producers\":\"\",\"genre\":\"Music & Dance\",\"subGenre\":\"General\",\"live\":false,\"premier\":false,\"ottBlackout\":false,\"highlight\":null,\"contentId\":null,\"contentImage\":null,\"groupKey\":null,\"vernacularData\":[]},{\"eventID\":\"24844565\",\"channelId\":21,\"channelStbNumber\":\"861\",\"channelHD\":\"false\",\"channelTitle\":\"Gold\",\"epgEventImage\":null,\"certification\":\"U\",\"displayDateTimeUtc\":\"2017-03-26 02:00:00.0\",\"displayDateTime\":\"2017-03-26 10:00:00.0\",\"displayDuration\":\"02:00:00\",\"siTrafficKey\":\"1:2169:30291205\",\"programmeTitle\":\"The Best Mix\",\"programmeId\":\"GBSSE\",\"episodeId\":\"\",\"shortSynopsis\":\"Best songs for the day.\",\"longSynopsis\":null,\"actors\":\"\",\"directors\":\"\",\"producers\":\"\",\"genre\":\"Music & Dance\",\"subGenre\":\"General\",\"live\":false,\"premier\":false,\"ottBlackout\":false,\"highlight\":null,\"contentId\":null,\"contentImage\":null,\"groupKey\":null,\"vernacularData\":[]},{\"eventID\":\"24844566\",\"channelId\":21,\"channelStbNumber\":\"861\",\"channelHD\":\"false\",\"channelTitle\":\"Gold\",\"epgEventImage\":null,\"certification\":\"U\",\"displayDateTimeUtc\":\"2017-03-26 04:00:00.0\",\"displayDateTime\":\"2017-03-26 12:00:00.0\",\"displayDuration\":\"03:00:00\",\"siTrafficKey\":\"1:2169:30291206\",\"programmeTitle\":\"Lunch Time with Oldies\",\"programmeId\":\"GBSSH\",\"episodeId\":\"\",\"shortSynopsis\":\"Mixture of '50s to '70s songs.\",\"longSynopsis\":null,\"actors\":\"\",\"directors\":\"\",\"producers\":\"\",\"genre\":\"Music & Dance\",\"subGenre\":\"General\",\"live\":false,\"premier\":false,\"ottBlackout\":false,\"highlight\":null,\"contentId\":null,\"contentImage\":null,\"groupKey\":null,\"vernacularData\":[]},{\"eventID\":\"24844567\",\"channelId\":21,\"channelStbNumber\":\"861\",\"channelHD\":\"false\",\"channelTitle\":\"Gold\",\"epgEventImage\":null,\"certification\":\"U\",\"displayDateTimeUtc\":\"2017-03-26 07:00:00.0\",\"displayDateTime\":\"2017-03-26 15:00:00.0\",\"displayDuration\":\"03:00:00\",\"siTrafficKey\":\"1:2169:30291207\",\"programmeTitle\":\"Your Favourites\",\"programmeId\":\"GBSSD\",\"episodeId\":\"\",\"shortSynopsis\":\"Giving you the songs you grew up with.\",\"longSynopsis\":null,\"actors\":\"\",\"directors\":\"\",\"producers\":\"\",\"genre\":\"Music & Dance\",\"subGenre\":\"General\",\"live\":false,\"premier\":false,\"ottBlackout\":false,\"highlight\":null,\"contentId\":null,\"contentImage\":null,\"groupKey\":null,\"vernacularData\":[]},{\"eventID\":\"24844568\",\"channelId\":21,\"channelStbNumber\":\"861\",\"channelHD\":\"false\",\"channelTitle\":\"Gold\",\"epgEventImage\":null,\"certification\":\"U\",\"displayDateTimeUtc\":\"2017-03-26 10:00:00.0\",\"displayDateTime\":\"2017-03-26 18:00:00.0\",\"displayDuration\":\"06:00:00\",\"siTrafficKey\":\"1:2169:30291208\",\"programmeTitle\":\"Starry Night\",\"programmeId\":\"GBSSF\",\"episodeId\":\"\",\"shortSynopsis\":\"More of the golden songs.\",\"longSynopsis\":null,\"actors\":\"\",\"directors\":\"\",\"producers\":\"\",\"genre\":\"Music & Dance\",\"subGenre\":\"General\",\"live\":false,\"premier\":false,\"ottBlackout\":false,\"highlight\":null,\"contentId\":null,\"contentImage\":null,\"groupKey\":null,\"vernacularData\":[]}]}" 
} 
} 
+3

まず、正しく解析します。 [サンプル](https://jsfiddle.net/g98p5rt7/)。第二に、あなたがエラーを起こしているなら、それを私たちと共有することができますか? – Rajesh

+1

あなたがしたいことは、2回のパスでこれを行うことだと思います。これは、ボディをストリング化し、それをメインオブジェクトに追加し、全体を再度ストリング化することによって作成されたようです。試してください: 'JSON.parse(JSON.parse(theObj).body)' –

+0

動作しませんでした。 JSFiddleを私に見せてください。 – Reza

答えて

2

それは本当に(ネットワーク要求または類似から、例えば、)文字列だと仮定すると、あなたはそれにJSON.parseを使用しています。次に、bodyプロパティがJSONを含む文字列であるため(たとえば、何らかの理由でその内容が二重に文字列化されているなど)、再度解析します。

例:

var json = '{\n' + 
 
' "timestamp": 1490545425158,\n' + 
 
' "reports": {\n' + 
 
' "statusCode": 200,\n' + 
 
' "body": "{\\"responseCode\\":\\"200\\",\\"responseMessage\\":\\"Success\\",\\"getevent\\":[{\\"eventID\\":\\"24844563\\",\\"channelId\\":21,\\"channelStbNumber\\":\\"861\\",\\"channelHD\\":\\"false\\",\\"channelTitle\\":\\"Gold\\",\\"epgEventImage\\":null,\\"certification\\":\\"U\\",\\"displayDateTimeUtc\\":\\"2017-03-25 16:00:00.0\\",\\"displayDateTime\\":\\"2017-03-26 00:00:00.0\\",\\"displayDuration\\":\\"06:00:00\\",\\"siTrafficKey\\":\\"1:2169:30291203\\",\\"programmeTitle\\":\\"Great Golden Oldies\\",\\"programmeId\\":\\"GBSSJ\\",\\"episodeId\\":\\"\\",\\"shortSynopsis\\":\\"Mixture of \'50s to \'70s songs.\\",\\"longSynopsis\\":null,\\"actors\\":\\"\\",\\"directors\\":\\"\\",\\"producers\\":\\"\\",\\"genre\\":\\"Music & Dance\\",\\"subGenre\\":\\"General\\",\\"live\\":false,\\"premier\\":false,\\"ottBlackout\\":false,\\"highlight\\":null,\\"contentId\\":null,\\"contentImage\\":null,\\"groupKey\\":null,\\"vernacularData\\":[]},{\\"eventID\\":\\"24844564\\",\\"channelId\\":21,\\"channelStbNumber\\":\\"861\\",\\"channelHD\\":\\"false\\",\\"channelTitle\\":\\"Gold\\",\\"epgEventImage\\":null,\\"certification\\":\\"U\\",\\"displayDateTimeUtc\\":\\"2017-03-25 22:00:00.0\\",\\"displayDateTime\\":\\"2017-03-26 06:00:00.0\\",\\"displayDuration\\":\\"04:00:00\\",\\"siTrafficKey\\":\\"1:2169:30291204\\",\\"programmeTitle\\":\\"Breakfast Time\\",\\"programmeId\\":\\"GBSSK\\",\\"episodeId\\":\\"\\",\\"shortSynopsis\\":\\"Wake up to Golden Oldies.\\",\\"longSynopsis\\":null,\\"actors\\":\\"\\",\\"directors\\":\\"\\",\\"producers\\":\\"\\",\\"genre\\":\\"Music & Dance\\",\\"subGenre\\":\\"General\\",\\"live\\":false,\\"premier\\":false,\\"ottBlackout\\":false,\\"highlight\\":null,\\"contentId\\":null,\\"contentImage\\":null,\\"groupKey\\":null,\\"vernacularData\\":[]},{\\"eventID\\":\\"24844565\\",\\"channelId\\":21,\\"channelStbNumber\\":\\"861\\",\\"channelHD\\":\\"false\\",\\"channelTitle\\":\\"Gold\\",\\"epgEventImage\\":null,\\"certification\\":\\"U\\",\\"displayDateTimeUtc\\":\\"2017-03-26 02:00:00.0\\",\\"displayDateTime\\":\\"2017-03-26 10:00:00.0\\",\\"displayDuration\\":\\"02:00:00\\",\\"siTrafficKey\\":\\"1:2169:30291205\\",\\"programmeTitle\\":\\"The Best Mix\\",\\"programmeId\\":\\"GBSSE\\",\\"episodeId\\":\\"\\",\\"shortSynopsis\\":\\"Best songs for the day.\\",\\"longSynopsis\\":null,\\"actors\\":\\"\\",\\"directors\\":\\"\\",\\"producers\\":\\"\\",\\"genre\\":\\"Music & Dance\\",\\"subGenre\\":\\"General\\",\\"live\\":false,\\"premier\\":false,\\"ottBlackout\\":false,\\"highlight\\":null,\\"contentId\\":null,\\"contentImage\\":null,\\"groupKey\\":null,\\"vernacularData\\":[]},{\\"eventID\\":\\"24844566\\",\\"channelId\\":21,\\"channelStbNumber\\":\\"861\\",\\"channelHD\\":\\"false\\",\\"channelTitle\\":\\"Gold\\",\\"epgEventImage\\":null,\\"certification\\":\\"U\\",\\"displayDateTimeUtc\\":\\"2017-03-26 04:00:00.0\\",\\"displayDateTime\\":\\"2017-03-26 12:00:00.0\\",\\"displayDuration\\":\\"03:00:00\\",\\"siTrafficKey\\":\\"1:2169:30291206\\",\\"programmeTitle\\":\\"Lunch Time with Oldies\\",\\"programmeId\\":\\"GBSSH\\",\\"episodeId\\":\\"\\",\\"shortSynopsis\\":\\"Mixture of \'50s to \'70s songs.\\",\\"longSynopsis\\":null,\\"actors\\":\\"\\",\\"directors\\":\\"\\",\\"producers\\":\\"\\",\\"genre\\":\\"Music & Dance\\",\\"subGenre\\":\\"General\\",\\"live\\":false,\\"premier\\":false,\\"ottBlackout\\":false,\\"highlight\\":null,\\"contentId\\":null,\\"contentImage\\":null,\\"groupKey\\":null,\\"vernacularData\\":[]},{\\"eventID\\":\\"24844567\\",\\"channelId\\":21,\\"channelStbNumber\\":\\"861\\",\\"channelHD\\":\\"false\\",\\"channelTitle\\":\\"Gold\\",\\"epgEventImage\\":null,\\"certification\\":\\"U\\",\\"displayDateTimeUtc\\":\\"2017-03-26 07:00:00.0\\",\\"displayDateTime\\":\\"2017-03-26 15:00:00.0\\",\\"displayDuration\\":\\"03:00:00\\",\\"siTrafficKey\\":\\"1:2169:30291207\\",\\"programmeTitle\\":\\"Your Favourites\\",\\"programmeId\\":\\"GBSSD\\",\\"episodeId\\":\\"\\",\\"shortSynopsis\\":\\"Giving you the songs you grew up with.\\",\\"longSynopsis\\":null,\\"actors\\":\\"\\",\\"directors\\":\\"\\",\\"producers\\":\\"\\",\\"genre\\":\\"Music & Dance\\",\\"subGenre\\":\\"General\\",\\"live\\":false,\\"premier\\":false,\\"ottBlackout\\":false,\\"highlight\\":null,\\"contentId\\":null,\\"contentImage\\":null,\\"groupKey\\":null,\\"vernacularData\\":[]},{\\"eventID\\":\\"24844568\\",\\"channelId\\":21,\\"channelStbNumber\\":\\"861\\",\\"channelHD\\":\\"false\\",\\"channelTitle\\":\\"Gold\\",\\"epgEventImage\\":null,\\"certification\\":\\"U\\",\\"displayDateTimeUtc\\":\\"2017-03-26 10:00:00.0\\",\\"displayDateTime\\":\\"2017-03-26 18:00:00.0\\",\\"displayDuration\\":\\"06:00:00\\",\\"siTrafficKey\\":\\"1:2169:30291208\\",\\"programmeTitle\\":\\"Starry Night\\",\\"programmeId\\":\\"GBSSF\\",\\"episodeId\\":\\"\\",\\"shortSynopsis\\":\\"More of the golden songs.\\",\\"longSynopsis\\":null,\\"actors\\":\\"\\",\\"directors\\":\\"\\",\\"producers\\":\\"\\",\\"genre\\":\\"Music & Dance\\",\\"subGenre\\":\\"General\\",\\"live\\":false,\\"premier\\":false,\\"ottBlackout\\":false,\\"highlight\\":null,\\"contentId\\":null,\\"contentImage\\":null,\\"groupKey\\":null,\\"vernacularData\\":[]}]}"\n' + 
 
' }\n' + 
 
'}'; 
 
var parsed = JSON.parse(json); 
 
parsed.reports.body = JSON.parse(parsed.reports.body); // This is the second parse 
 
console.log(parsed);

しかし:理想的には、不必要に二重文字列化bodyあるソースを修正したいと思います。もちろん

、何かがすでに、その後の進路あなたはその最初の解析を必要としない、(例えば、多くのライブラリが自動的にAJAXを介して検索JSONをパースします)あなたのための最初の解析を行うの場合:

var parsed = { 
 
    "timestamp": 1490545425158, 
 
    "reports": { 
 
    "statusCode": 200, 
 
    "body": "{\"responseCode\":\"200\",\"responseMessage\":\"Success\",\"getevent\":[{\"eventID\":\"24844563\",\"channelId\":21,\"channelStbNumber\":\"861\",\"channelHD\":\"false\",\"channelTitle\":\"Gold\",\"epgEventImage\":null,\"certification\":\"U\",\"displayDateTimeUtc\":\"2017-03-25 16:00:00.0\",\"displayDateTime\":\"2017-03-26 00:00:00.0\",\"displayDuration\":\"06:00:00\",\"siTrafficKey\":\"1:2169:30291203\",\"programmeTitle\":\"Great Golden Oldies\",\"programmeId\":\"GBSSJ\",\"episodeId\":\"\",\"shortSynopsis\":\"Mixture of '50s to '70s songs.\",\"longSynopsis\":null,\"actors\":\"\",\"directors\":\"\",\"producers\":\"\",\"genre\":\"Music & Dance\",\"subGenre\":\"General\",\"live\":false,\"premier\":false,\"ottBlackout\":false,\"highlight\":null,\"contentId\":null,\"contentImage\":null,\"groupKey\":null,\"vernacularData\":[]},{\"eventID\":\"24844564\",\"channelId\":21,\"channelStbNumber\":\"861\",\"channelHD\":\"false\",\"channelTitle\":\"Gold\",\"epgEventImage\":null,\"certification\":\"U\",\"displayDateTimeUtc\":\"2017-03-25 22:00:00.0\",\"displayDateTime\":\"2017-03-26 06:00:00.0\",\"displayDuration\":\"04:00:00\",\"siTrafficKey\":\"1:2169:30291204\",\"programmeTitle\":\"Breakfast Time\",\"programmeId\":\"GBSSK\",\"episodeId\":\"\",\"shortSynopsis\":\"Wake up to Golden Oldies.\",\"longSynopsis\":null,\"actors\":\"\",\"directors\":\"\",\"producers\":\"\",\"genre\":\"Music & Dance\",\"subGenre\":\"General\",\"live\":false,\"premier\":false,\"ottBlackout\":false,\"highlight\":null,\"contentId\":null,\"contentImage\":null,\"groupKey\":null,\"vernacularData\":[]},{\"eventID\":\"24844565\",\"channelId\":21,\"channelStbNumber\":\"861\",\"channelHD\":\"false\",\"channelTitle\":\"Gold\",\"epgEventImage\":null,\"certification\":\"U\",\"displayDateTimeUtc\":\"2017-03-26 02:00:00.0\",\"displayDateTime\":\"2017-03-26 10:00:00.0\",\"displayDuration\":\"02:00:00\",\"siTrafficKey\":\"1:2169:30291205\",\"programmeTitle\":\"The Best Mix\",\"programmeId\":\"GBSSE\",\"episodeId\":\"\",\"shortSynopsis\":\"Best songs for the day.\",\"longSynopsis\":null,\"actors\":\"\",\"directors\":\"\",\"producers\":\"\",\"genre\":\"Music & Dance\",\"subGenre\":\"General\",\"live\":false,\"premier\":false,\"ottBlackout\":false,\"highlight\":null,\"contentId\":null,\"contentImage\":null,\"groupKey\":null,\"vernacularData\":[]},{\"eventID\":\"24844566\",\"channelId\":21,\"channelStbNumber\":\"861\",\"channelHD\":\"false\",\"channelTitle\":\"Gold\",\"epgEventImage\":null,\"certification\":\"U\",\"displayDateTimeUtc\":\"2017-03-26 04:00:00.0\",\"displayDateTime\":\"2017-03-26 12:00:00.0\",\"displayDuration\":\"03:00:00\",\"siTrafficKey\":\"1:2169:30291206\",\"programmeTitle\":\"Lunch Time with Oldies\",\"programmeId\":\"GBSSH\",\"episodeId\":\"\",\"shortSynopsis\":\"Mixture of '50s to '70s songs.\",\"longSynopsis\":null,\"actors\":\"\",\"directors\":\"\",\"producers\":\"\",\"genre\":\"Music & Dance\",\"subGenre\":\"General\",\"live\":false,\"premier\":false,\"ottBlackout\":false,\"highlight\":null,\"contentId\":null,\"contentImage\":null,\"groupKey\":null,\"vernacularData\":[]},{\"eventID\":\"24844567\",\"channelId\":21,\"channelStbNumber\":\"861\",\"channelHD\":\"false\",\"channelTitle\":\"Gold\",\"epgEventImage\":null,\"certification\":\"U\",\"displayDateTimeUtc\":\"2017-03-26 07:00:00.0\",\"displayDateTime\":\"2017-03-26 15:00:00.0\",\"displayDuration\":\"03:00:00\",\"siTrafficKey\":\"1:2169:30291207\",\"programmeTitle\":\"Your Favourites\",\"programmeId\":\"GBSSD\",\"episodeId\":\"\",\"shortSynopsis\":\"Giving you the songs you grew up with.\",\"longSynopsis\":null,\"actors\":\"\",\"directors\":\"\",\"producers\":\"\",\"genre\":\"Music & Dance\",\"subGenre\":\"General\",\"live\":false,\"premier\":false,\"ottBlackout\":false,\"highlight\":null,\"contentId\":null,\"contentImage\":null,\"groupKey\":null,\"vernacularData\":[]},{\"eventID\":\"24844568\",\"channelId\":21,\"channelStbNumber\":\"861\",\"channelHD\":\"false\",\"channelTitle\":\"Gold\",\"epgEventImage\":null,\"certification\":\"U\",\"displayDateTimeUtc\":\"2017-03-26 10:00:00.0\",\"displayDateTime\":\"2017-03-26 18:00:00.0\",\"displayDuration\":\"06:00:00\",\"siTrafficKey\":\"1:2169:30291208\",\"programmeTitle\":\"Starry Night\",\"programmeId\":\"GBSSF\",\"episodeId\":\"\",\"shortSynopsis\":\"More of the golden songs.\",\"longSynopsis\":null,\"actors\":\"\",\"directors\":\"\",\"producers\":\"\",\"genre\":\"Music & Dance\",\"subGenre\":\"General\",\"live\":false,\"premier\":false,\"ottBlackout\":false,\"highlight\":null,\"contentId\":null,\"contentImage\":null,\"groupKey\":null,\"vernacularData\":[]}]}" 
 
} 
 
}; 
 
parsed.reports.body = JSON.parse(parsed.reports.body); // Parse body 
 
console.log(parsed);

+0

お返事ありがとうございました。 – Reza

0

あなたは体を解析する必要があります。

JSON.parse(object.reports.body)

残りはすでに "解析された" です。

enter image description here

+0

すべてが有効なJSONです。 –

+1

エディタで '<>'アイコンを確認してください。 * JS/CSS/HTML *コードを置いて実行可能なスニペットにすることができます。 – Rajesh

+0

ボディパーツの解析方法は?それが私が求めていたものです。 – Reza

関連する問題