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'
>>

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 JSON Format에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by