次の方法で共有


Working with JSON Data

Microsoft Silverlight will reach end of support after October 2021. Learn more.

HTTP-based Web services frequently use JavaScript Object Notation (JSON) messages to return data back to the client. JSON objects can easily be instantiated in JavaScript, without parsing logic, which makes it a convenient format for messages in Web applications.

Assume that after making a request to the HTTP-based Web service as described in How to: Make Requests to HTTP-Based Services, the following JSON is returned inside a responseStream object of type Stream.

{"IsMember" : true, "Name" : "John", "Age" : 24}

Here are two techniques available in Silverlight version 4 to parse this response.

Using DataContractJsonSerializer

Define the User type to deserialize the JSON into.

public class User
{
    public bool IsMember { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

There is no need to mark a type or the members of that type with special attributes to opt into the deserialization—all public members are included automatically in the data contract. This is a simplification of the data contract programming model, which required an explicit DataMemberAttribute attribute for each member and a DataContractAttribute attribute for each type to be included in the data contract. The current programming model enables a member’s name and visibility to determine whether it is serialized.

The following code shows how to create an instance of the DataContractJsonSerializer and use it to deserialize a responseStream into a User object.

DataContractJsonSerializer serializer = 
    new DataContractJsonSerializer(typeof(User));
User user = (User)serializer.ReadObject(responseStream);

bool isMember = user.IsMember;
string name = user.Name;
int age = user.Age;

Using JsonObject and LINQ

Silverlight provides the JsonPrimitive, JsonArray, and JsonObject types, which allow you to treat JSON in a weakly typed manner. This approach allows you to dynamically access the values of primitive JSON types (string, number, Boolean) and index into structured JSON types (object and array), without having to predefine types (such as the User type) to deserialize into. The following code shows an example of how this can be done.

JsonObject user = (JsonObject)JsonObject.Load(responseStream);
bool isMember = user["IsMember"];
string name = user["Name"];
int age = user["Age"];

Using JsonObject with LINQ allows us to easily deal with complex JSON objects. Assume, for example, that the following JSON is contained in the responseStream object of type Stream.

[{"IsMember" : true, "Name" : "John", "Age" : 24}, 
{"IsMember" : false, "Name" : "Paul", "Age" : 44},
{"IsMember" : true, "Name" : "George", "Age" : 12}]

To retrieve all people who are members, use the following LINQ query over a JsonArray object.

JsonArray users = (JsonArray)JsonArray.Load(responseStream);

var members = from member in users
              where member["IsMember"]
              select member;

foreach (JsonObject member in members)
{
   string name = member["Name"];
   int age = member["Age"];

   // Do something...

}

See Also