Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

My Json object looks like below,
JavaScript
[{"key":"field1","value":false},{"key":"field2","value":false},{"key":"field3","value":false},{"key":"field4","value":false},{"key":"field7","value":false},{"key":"field8","value":false}]

I am deserialize it like

C#
JavaScriptSerializer serialize = new JavaScriptSerializer();
List<KeyValuePair<string, string>> obj = serialize.Deserialize<List<KeyValuePair<string, string>>>(Element);


But getting empty list, I don't know why , Am I missinbg anything?

Thanks,
Rachana
Posted

1 solution

I don't see a whole lot wrong with the code so I'm thinking your problem might be with the serialization itself. I have several websites written in PHP that I use JSON serialization and encrypted data to pull down orders that have come in through the sites instead of going to each database individually to process the orders. I accomplish this through Data Contracts in a C# app.
I've attached some snippets from a couple of my classes for example I hope they help answer your question.

First I set up a my data contract class
C#
using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;

namespace WebOrders
{
    [DataContract]
    public class OrderItem
    {
        public OrderItem() { }

        [DataMember]
        public string orderID { get; set; }

        [DataMember]
        public string qty { get; set; }

        [DataMember]
        public string productID { get; set; }

        [DataMember]
        public string mfrPartNumber { get; set; }

        [DataMember]
        public string price { get; set; }
    }
}


Then I use a utility class that has a generic enumerator to handle all serialization and deserialization transactions...

C#
public static IEnumerable<t> GetValues<t>()
{
    return Enum.GetValues(typeof(T)).Cast<t>();
}


public static string Serialize<t>(T obj)
{
    System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
    MemoryStream ms = new MemoryStream();
    serializer.WriteObject(ms, obj);
    string ret = Encoding.Default.GetString(ms.ToArray());
    ms.Dispose();
    return ret;
}

public static T Deserialize<t>(string json)
{
    T obj = Activator.CreateInstance<t>();
    MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
    System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
    obj = (T)serializer.ReadObject(ms);
    ms.Close();
    ms.Dispose();
    return obj;
}


Usage is as follows:
C#
OrderItem orderitem = Deserialize<OrderItem>(o);


Hope you find this useful, I know I sure did...
-Smith
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900