I'm trying to get an NSData from a json string, to use it later to create an MGLShape (From Mapbox SDK) like this:
MGLShape.ShapeWithData(jsonData, 4, out error); //jsonData is the NSData, 4 is the nuint for the type of encoding and ou error is a plain NSError.
But i'm not able to serialize the json string into NSData using NSJsonSerialization, the json string is corrected and validated before, (but when i use an NSString from the string, it adds an extra pair of brackets "{ //json }", which i can remove before trying the NSJsonSerialization, here is how i'm trying to achive my goal:
string jsonSerialized = JsonConvert.SerializeObject(fc);//Valid Json NSString json = new NSString(jsonSerialized);//Adds the extra pair of brackets NSData jsonData = NSJsonSerialization.Serialize(json, NSJsonWritingOptions.SortedKeys, out error);
But it gives me the following error:
Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInvalidArgumentException Reason: *** +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write
This error happens with, or without the extra pair of brackets (removed the first and last chars from the NSString), here is a shortned version of my json:
(i used https://jsonformatter.curiousconcept.com/ to test the json)
{"type":"FeatureCollection","crs":null,"features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[-9.000000,38.000000]},"properties":{"id":1,"icon":"MyIcon.png"}}]}
What am i doing wrong here? how can i parse an json string into an NSData?
I found a solution.. quite simple:
NSData jsonData = NSData.FromString(jsonSerialized, NSStringEncoding.UTF8);
NSData Serialize(NSObject obj, NSJsonWritingOptions opt, out NSError error);
obj
here should be NSDictionary
not NSString
,check https://stackoverflow.com/a/30618924/8187800.
I tried the following code , it works fine .
NSDictionary json = NSDictionary.FromObjectAndKey(new NSString("a") , new NSString("a")); NSData jsonData = NSJsonSerialization.Serialize(json, NSJsonWritingOptions.SortedKeys, out error);
So you major problem is how to convert json string to NSDictionary
.
Answers
I found a solution.. quite simple:
NSData Serialize(NSObject obj, NSJsonWritingOptions opt, out NSError error);
obj
here should beNSDictionary
notNSString
,check https://stackoverflow.com/a/30618924/8187800.I tried the following code , it works fine .
So you major problem is how to convert json string to
NSDictionary
.@ColeX it worked too! but i found my solution to be more simple
Of course ,your solution is the direct method from NSString to NSData.
i was overcomplicating things up, thank you anyway for your time
Hi @colex,
Am using naxam mapbox and struggling to load coordinates from json in ios . Here, my code tried in ios xamarin.
I completed in android.
My requirement is to fill color to full US region. But in ios alone my failing some how. Can you please find and let me know what am doing wrong.
Hi @ColeX /@Ricardo,
How do i can calculate nunit value given for MGLShape date
Hi @JoannaG, this is what i think you are looking for:
first i created this classes with the geojson property names, that will make it easier to serialize to the geojson source:
then i created this function that returns the geojson, by serializing the list of MapFeaturePoint:
then you just need to use the shape when creating the source (added the code for geojson source options also):
And that's how i add the source for the layers