Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I de/serialize JSON in WinRT?

Tags:

c#

winrt-xaml

How do I take an object and convert it to a JSON string and then back into that object from a string, specifically, in WinRT for my Windows 8 Metro application?

like image 893
Jerry Nixon Avatar asked Jun 10 '12 01:06

Jerry Nixon


People also ask

How do I deserialize JSON?

A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.

What is serializing a JSON?

Json namespace provides functionality for serializing to and deserializing from JavaScript Object Notation (JSON). Serialization is the process of converting the state of an object, that is, the values of its properties, into a form that can be stored or transmitted.

Do we need to serialize JSON?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object). If you serialize this result it will generate a text with the structure and the record returned.

What is JSON deserialization in C#?

Serialization is the process of converting . NET objects such as strings into a JSON format and deserialization is the process of converting JSON data into . NET objects.


2 Answers

Like this:

using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;

public static T Deserialize<T>(string json)
{
    var _Bytes = Encoding.Unicode.GetBytes(json);
    using (MemoryStream _Stream = new MemoryStream(_Bytes))
    {
        var _Serializer = new DataContractJsonSerializer(typeof(T));
        return (T)_Serializer.ReadObject(_Stream);
    }
}

public static string Serialize(object instance)
{
    using (MemoryStream _Stream = new MemoryStream())
    {
        var _Serializer = new DataContractJsonSerializer(instance.GetType());
        _Serializer.WriteObject(_Stream, instance);
        _Stream.Position = 0;
        using (StreamReader _Reader = new StreamReader(_Stream)) 
        { return _Reader.ReadToEnd(); }
    }
}
like image 53
Jerry Nixon Avatar answered Sep 30 '22 19:09

Jerry Nixon


First generate C# classes with http://json2csharp.com/ Then use http://james.newtonking.com/pages/json-net.aspx for parsing

My user class currently looks like this:

public class User
{
    public string id { get; set; }
    public string username { get; set; }
    public string full_name { get; set; }
    public string profile_picture { get; set; }
    public string bio { get; set; }
    public string website { get; set; }
    public Counts counts { get; set; }

    public static User SingleFromJSON(string jsonString)
    {
        return JsonConvert.DeserializeObject<SingleUser>(jsonString).data;
    }

    public static User MultipleFromJSON(string jsonString)
    {
        return JsonConvert.DeserializeObject<SingleUser>(jsonString).data;
    }

    private class SingleUser
    {
        public User data { get; set; }
    }

    private class MultipleUsers
    {
        public List<User> data { get; set; }
    }
}

public class Counts
{
    public int media { get; set; }
    public int follows { get; set; }
    public int followed_by { get; set; }
}

Super easy :D

like image 20
softarn Avatar answered Sep 30 '22 18:09

softarn