JSON vs. XML using webread()

4 ビュー (過去 30 日間)
Jeffrey
Jeffrey 2015 年 8 月 2 日
コメント済み: Kevin Lapret 2015 年 8 月 4 日
I have a url that I am able to get data when I set the output to xml as an optional parameter in the url, but when I set it to json, I get NaN... the url is 'http://api.eia.gov/series/?api_key=####&series_id=NG.RNGC1.D&out=json' where #### is a private key. Any ideas? I am able to get a struct array when I use the World Bank Climate example in Matlab help documentation.
  1 件のコメント
Kevin Lapret
Kevin Lapret 2015 年 8 月 4 日
Not sure what you will be doing with the JSON data but if you want to convert it to a more import friendly format, there are some JSON to CSV converters like json-csv.com

サインインしてコメントする。

採用された回答

Robert Snoeberger
Robert Snoeberger 2015 年 8 月 3 日
The JSON text received from the web service contains the UTF-8 byte order mark (BOM), , at the beginning [1]. The BOM is causing the JSON parser to fail. You can see the BOM by calling webread with a content type of text.
>> url = 'http://api.eia.gov/series/?api_key=####&series_id=NG.RNGC1.D&out=json';
>> webread(url, weboptions('ContentType', 'text'))
ans =
{"request":{"command":"series"},"data":{"error":"No api_key. For key registration, documentation, and examples see http:\/\/www.eia.gov\/developer\/"}}
>>
[1] See byte order mark for UTF-8 encoding in Representation of byte order marks by encoding

その他の回答 (1 件)

Jeffrey
Jeffrey 2015 年 8 月 3 日
That's very helpful, thank you...

カテゴリ

Help Center および File ExchangeJSON Format についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by