Reputation: 199
I am creating a Windows 8 App in C# using Visual Studio. I am trying to take a bunch of data input and create a JSON object from that data. Here is my preliminary test method that does not work:
IJsonValue name = "Alex";
JsonObject Character = new JsonObject();
Character.Add("Name", name);
The error that I am getting is
Cannot Implicitly convert type 'string' to 'Windows.Data.Json.IJsonValue'
I looked up the documentation for IJsonValue but couldn't figure out how to create an instance of IJsonValue containing a string. So how do I store data in an IJsonValue to be added to a JsonObject?
Upvotes: 0
Views: 3073
Reputation: 10095
Here's how you would do it:
JsonObject character = new JsonObject();
character.Add("Name",JsonValue.CreateStringValue("Alex"));
Upvotes: 4
Reputation: 45117
The class JsonValue implements the IJsonValue interface. You can create an instance of the class and use it like this for example ...
JsonValue jsonValue = JsonValue.Parse("{\"Width\": 800, \"Height\": 600, \"Title\": \"View from 15th Floor\", \"IDs\": [116, 943, 234, 38793]}");
double width = jsonValue.GetObject().GetNamedNumber("Width");
double height = jsonValue.GetObject().GetNamedNumber("Height");
string title = jsonValue.GetObject().GetNamedString("Title");
JsonArray ids = jsonValue.GetObject().GetNamedArray("IDs");
Check out this for more examples.
Upvotes: 3