user1302818
user1302818

Reputation: 49

Serialize JSON Random Name

{
    "123353054": "value here",
    "username": "value here"
}

Is there any way to serialize this in C#?

Upvotes: 2

Views: 431

Answers (1)

Davan Etelamaki
Davan Etelamaki

Reputation: 181

You can serialize this into a dynamic or create a static class and use it for serialization using the JavaScriptSerializer to Deserialize it into C#.

If you want to find the structure you need in C# for the Serializer to serialize this to JavaScript create a dynamic variable and then serialize to that variable and inspect it in debug and it will show the static structure.

I can give an example of the code required if you need it.

using System;
using System.Web.Script.Serialization;

namespace JSON_Serialization_Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            const string json = "{'123353054': 'value here','username': 'value here'}";
            var jss = new JavaScriptSerializer();

            var csobj = jss.Deserialize<dynamic>(json);
            Console.WriteLine(csobj.GetType());
            Console.Read();
        }
    }
}

Testing this shows that your item with be a dictionary in C# in order to serialize back to JSON correctly.

Upvotes: 2

Related Questions