Webwrite not encoding JSON the way I want
3 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 JSON Format 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!