名前を二重引用符で囲まずに、無効な外部jsonデータがあります。
例:
{
data: [
{
idx: 0,
id: "0",
url: "http://247wallst.com/",
a: [
{
t: "Title",
u: "http://247wallst.com/2012/07/30/",
sp: "About"
}
],
doc_id: "9386093612452939480"
},
{
idx: 1,
id: "-1"
}
],
results_per_page: 10,
total_number_of_news: 76,
news_per_month: [20, 0, 8, 1, 1, 2, 0, 2, 1, 0, 0, 1, 1, 0, 5, 1, 1, 1, 0, 2, 5, 16, 7, 1],
result_start_num: 2,
result_end_num: 2,
result_total_articles: 76
}
ご覧のとおり、data、idx、id、urlなどの多くの名前は二重引用符で囲まれていないため、このjsonは無効になります。この外部JSONを有効にするにはどうすればよいですか?私はすでにstr_replaceを試し、'{'を'{"'に、':'を'":'に置き換え、引用符で囲まれていない名前の前後に二重引用符を追加しましたが、これはすでに二重引用符で囲まれた変数を台無しにします。
このjsonを有効にして、PHP json_decodeでこのデータを読み取れるようにするにはどうすればよいですか?私はpreg_replaceにあまり精通していません。
有効なjsonは次のようになります。
{
"data": [
{
"idx": 0,
"id": "0",
"url": "http://247wallst.com/",
"a": [
{
"t": "Title",
"u": "http://247wallst.com/2012/07/30/",
"sp": "About"
}
],
"doc_id": "9386093612452939480"
},
{
"idx": 1,
"id": "-1"
}
],
"results_per_page": 10,
"total_number_of_news": 76,
"news_per_month": [20, 0, 8, 1, 1, 2, 0, 2, 1, 0, 0, 1, 1, 0, 5, 1, 1, 1, 0, 2, 5, 16, 7, 1],
"result_start_num": 2,
"result_end_num": 2,
"result_total_articles": 76
}
phppreg_replace関数を提案してください。
データソース: http ://www.google.com/finance/company_news?q = aapl&output = json&start = 1&num = 1