Webwrite not encoding JSON the way I want

3 ビュー (過去 30 日間)
Daniel LaCroix
Daniel LaCroix 2015 年 12 月 6 日
コメント済み: Daniel LaCroix 2015 年 12 月 7 日
I'm trying to post a geoJSON object to a web API. I found a website that will tell me if my geoJSON is formatted correctly or not for testing. I also am using a website that will return whatever a post to it, so I can see how MATLAB formatted stuff.
Here is my code:
options = weboptions('MediaType','application/json','RequestMethod','post');
data.type='Polygon';
data.coordinates=[-84,42;-84,43;-83,43;-83,42;-84,42];
test=webwrite('http://geojsonlint.com/validate',data,options) %checks if geoJSON is valid
raw=webwrite('http://httpbin.org/post',data,options) %returns the request that was posted
raw.data shows what was posted. Copying and pasting that at http://geojsonlint.com/ also results in an error, as did the webwrite command. However, adding another set of square brackets around the coordinates fixes the problem. (Making it {"type":"Polygon","coordinates":[[[-84,42],[-84,43],[-83,43],[-83,42],[-84,42]]]} ). I can't figure out how to get MATLAB to add those brackets. Making it a string results in a different error.
So there's two things that would answer my question: how do I make make MATLAB encode this properly, or how to I post my own JSON? I've had problems with MATLAB encoding JSON before. I can often figure out how to format the JSON myself, but don't know how to post that to a website or API.

採用された回答

Robert Snoeberger
Robert Snoeberger 2015 年 12 月 6 日
>> options = weboptions('MediaType','application/json','RequestMethod','post');
>> data.type='Polygon';
>> data.coordinates={[-84,42;-84,43;-83,43;-83,42;-84,42]};
>> test=webwrite('http://geojsonlint.com/validate',data,options)
test =
status: 'ok'
>>
  1 件のコメント
Daniel LaCroix
Daniel LaCroix 2015 年 12 月 7 日
Thanks!

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

その他の回答 (0 件)

カテゴリ

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