Webwrite not encoding JSON the way I want

3 vues (au cours des 30 derniers jours)
Daniel LaCroix
Daniel LaCroix le 6 Déc 2015
Commenté : Daniel LaCroix le 7 Déc 2015
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.

Réponse acceptée

Robert Snoeberger
Robert Snoeberger le 6 Déc 2015
>> 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'
>>

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by