Condividi tramite


Using JavaScript Object Notation (JSON) (Windows Runtime app using C++, C#, or Visual Basic)

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

Purpose

Parse values, strings, objects, and arrays from JavaScript Object Notation (JSON) text or serialize value types into JSON text using features in the Windows.Data.Json namespace.

JSON is an open, text-based data exchange format (see RFC 4627). It is platform independent and enjoys a wide availability of implementations. Data formatted according to the JSON standard is lightweight and can be parsed by JavaScript implementations with incredible ease. Since it is primarily a data format, JSON can be used in virtually any scenario where applications need to exchange or store structured information as text.

The following features can be used in a Windows Runtime app to retrieve text that contains JSON from an HTTP server or send text that contains JSON to an HTTP server.

Parsing JSON and serializing data into JSON

The following sections will demonstrate how to parse JSON text for number, string, object, and array values and represent each as manageable objects or take values and convert them to objects and serialize them into JSON.

Objects

Deserialize an object from JSON text into a JsonObject and retrieve the named values that it contains.

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");
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");

Create a new JsonObject and populate it with new name/JsonValue pairs using the JsonObject::Insert method. Once finished, call JsonObject::Stringify to serialize the JsonObject as JSON text.

JsonObject jsonObject = new JsonObject();
jsonObject["Width"] = JsonValue.CreateNumberValue(800);
jsonObject["Height"] = JsonValue.CreateNumberValue(600);
jsonObject["Title"] = JsonValue.CreateStringValue("View from 15th Floor");
jsonObject["IDs"] = JsonValue.Parse("[116, 943, 234, 38793]");
string jsonString = jsonObject.Stringify();
JsonObject^ jsonObject = ref new JsonObject();
jsonObject->Insert("Width", JsonValue::CreateNumberValue(800));
jsonObject->Insert("Height", JsonValue::CreateNumberValue(600));
jsonObject->Insert("Title", JsonValue::CreateStringValue("View from 15th Floor"));
jsonObject->Insert("IDs", JsonValue::Parse("[116, 943, 234, 38793]"));
String^ jsonString = jsonObject->Stringify();

Arrays

Deserialize an array from JSON text into a JsonArray and retrieve the indexed values that it contains.

JsonValue jsonValue = JsonValue.Parse("[116, 3.14159, true, \"abc\"]");
double element1 = jsonValue.GetArray().GetNumberAt(0);
double element2 = jsonValue.GetArray().GetNumberAt(1);
bool element3 = jsonValue.GetArray().GetBooleanAt(2);
string element4 = jsonValue.GetArray().GetStringAt(3);
JsonValue^ jsonValue = JsonValue::Parse("[116, 3.14159, true, \"abc\"]");
double element1 = jsonValue->GetArray()->GetNumberAt(0);
double element2 = jsonValue->GetArray()->GetNumberAt(1);
bool element3 = jsonValue->GetArray()->GetBooleanAt(2);
String^ element4 = jsonValue->GetArray()->GetStringAt(3);

Alternatively, values contained in a JsonArray can be retrieved using the GetAt method. In the following example each indexed value is run through a switch case statement, the ValueType property expressed within each JsonValue determining the specific method used to retrieve the value.

JsonValue jsonValue = JsonValue.Parse("[116, 3.14159, true, \"abc\"]");
int arraySize = jsonValue.GetArray().Count;
for (int i = 0; i < arraySize; i++)
{
    IJsonValue element = jsonValue.GetArray()[i];
    switch (element.ValueType)
    {
       case JsonValueType.Number:
           double numberValue = element.GetNumber();
           break;
       // ...
    }
}
JsonValue^ jsonValue = JsonValue::Parse("[116, 3.14159, true, \"abc\"]");
int arraySize = jsonValue->GetArray()->Size;
for (int i = 0; i < arraySize; i++)
{
     IJsonValue^ element = jsonValue->GetArray()->GetAt(i);
     switch (element->ValueType)
     {
     case JsonValueType::Number:
            double numberValue = element->GetNumber();
            break;
            // ...
     }
}

Create a new JsonArray object and populate it with JsonValue objects using the JsonArray::Append method. Once finished, call JsonArray::Stringify to serialize the JsonArray as JSON text.

JsonArray jsonArray = new JsonArray();
jsonArray.Add(JsonValue.CreateNumberValue(116));
jsonArray.Add(JsonValue.CreateNumberValue(3.14159));
jsonArray.Add(JsonValue.CreateBooleanValue(true));
jsonArray.Add(JsonValue.CreateStringValue("abc"));
string jsonString = jsonArray.Stringify();
JsonArray^ jsonArray = ref new JsonArray();
jsonArray->Append(JsonValue::CreateNumberValue(116));
jsonArray->Append(JsonValue::CreateNumberValue(3.14159));
jsonArray->Append(JsonValue::CreateBooleanValue(true));
jsonArray->Append(JsonValue::CreateStringValue("abc"));
String^ jsonString = jsonArray->Stringify();

Numbers

Parse a number value from JSON text, and retrieve the number from the resulting JsonValue using JsonValue::GetNumber

JsonValue jsonValue = JsonValue.Parse("5");
double numberValue = jsonValue.GetNumber();
JsonValue^ jsonValue = JsonValue::Parse("5");
double numberValue = jsonValue->GetNumber();

To serialize a number value as JSON text, create a new JsonValue and assign desired value with JsonValue::CreateNumberValue.

JsonValue jsonValue = JsonValue.CreateNumberValue(5);
string jsonString = jsonValue.Stringify();
JsonValue^ jsonValue = JsonValue::CreateNumberValue(5);
String^ jsonString = jsonValue->Stringify();

Strings

Parse a string value from JSON text, and retrieve the string from the resulting JsonValue using JsonValue::GetString

JsonValue jsonValue = JsonValue.Parse("\"test string\"");
string stringValue = jsonValue.GetString();
JsonValue^ jsonValue = JsonValue::Parse("\"test string\"");
String^ stringValue = jsonValue->GetString();

To serialize a string value as JSON text, create a new JsonValue and assign desired value with JsonValue::CreateStringValue.

JsonValue jsonValue = JsonValue.CreateStringValue("test string");
string jsonString = jsonValue.Stringify();
JsonValue^ jsonValue = JsonValue::CreateStringValue("test string");
String^ jsonString = jsonValue->Stringify();

Other resources

Parse JSON Strings in Windows Runtime Components

Reference

C++ REST SDK

JsonArray

JsonObject

JsonValue

Windows.Data.Json

Windows.Web.Http

Windows.Web.Http.HttpClient

XML HTTP Extended Request (IXMLHttpRequest2)

Samples

Json Sample