Webwrite not encoding JSON the way I want
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Daniel LaCroix
am 6 Dez. 2015
Kommentiert: Daniel LaCroix
am 7 Dez. 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.
0 Kommentare
Akzeptierte Antwort
Robert Snoeberger
am 6 Dez. 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'
>>
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu JSON Format finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!