Geeks With Blogs

@Sorskoot
  • Sorskoot "Visual Studio 2013, will be available later this year, with a preview build publicly available at Build" http://t.co/SV28646Qr6 about 514 days ago

News


Timmy Kokke's Blog

↑ Grab this Headline Animator

Timmy Kokke at Blogged
Timmy Kokke …just sorting my bubbles…

silverbullet I’d like to provide you with a SilverBullet™, a small snippet of Silverlight, a class or namespace hidden in the silverlight .NET framework, to help you out in times of need. It’s not to learn, but something to keep in your pocket. Just remember it’s there and you’re safe.

 

 

The System.Json namespace provides support for Javascript Object Notation or JSON. By using the classes in this namespace you are able to use Json objects in your C# or VB code. These object are often used in JavaScript and to send to and be returned by services.

 

The namespace contains four classes. The first, JsonValue is the base class for the other three. It has a few methods to load, save and parse strings and streams. The ToString method is overloaded to return text based Json.

 

The JsonPrimitive class represents a Json primitive type in the CLR. For example a byte, string or int. It’s constructed like:

var pInt = new JsonPrimitive(25); 
var pString = new JsonPrimitive("abc");
var pDouble = new JsonPrimitive(3.14);

The JsonArray class represents an array of JsonValues. Using the JsonPrimitives from the earlier example, JsonArray can be constructed and used like:

var ar = new JsonArray {pInt, pString, pDouble};
output.Text = ar.ToString();

After running this, the output.Text value will contain: 

[25,"abc",3.14]

 

The JsonObject represents a collection of key/pair values. Using the same values again, JsonObject can be constructed and used like:

var ob = new JsonObject {
            {"pInt", pInt}, 
            {"pString", pString}, 
            {"pDouble", pDouble}
        };
output2.Text = ob.ToString();

In this case, output2.Text will contain:

{"pInt":25,"pString":"abc","pDouble":3.14}

Last thing I would like to mention is the JsonType enumeration. This enum is returned by the JsonType property on the JsonValue class. And represents the type of CLR object the JsonValue instance is representing.

 

Shout it
Tags van Technorati:
dotNed blogger
kick it on DotNetKicks.com
Posted on Tuesday, June 16, 2009 7:08 PM Silverlight , SilverBullet | Back to top


Comments on this post: SilverBullet #2 – System.Json

# re: SilverBullet #2 – System.Json
Requesting Gravatar...
Great... Didn't know about JSON in Silverlight. Thanks! :)
Left by Danijel on Jun 12, 2009 4:25 AM

Your comment:
 (will show your gravatar)
 


Copyright © Timmy Kokke | Powered by: GeeksWithBlogs.net | Join free