2

I have an object like below to be deserialized in C#. I am wondering how I can parse it. I tried following this example here, but am stumped on how I can get my class to recognize the key of each object (the 2 and the 3 below).

The JSON string below basically represents 2 transactions. I would like to convert each transaction representation into a Transaction object and put it into an array of Transaction object.

{
    "2": {
        "id": "2",
        "user_id": "59",
        "offer_id": "1234"
    },
    "3": {
        "id": "3",
        "user_id": "59",
        "offer_id": "1234"
    }
}

Here are my classes:

public class Transactions
{
    // what goes here since the "key" field is always different?
}

public class Transaction
{
    public int id { get; set; }
    public int user_id { get; set; }
    public int offer_id { get; set; }
}
Community
  • 1
  • 1
DLS
  • 5,313
  • 8
  • 37
  • 50
  • 1
    you are aware that 2 and 3 are objects, not array keys? – KyorCode Oct 09 '14 at 05:36
  • Yes. I am. Am I missing something here that is completely clear? – DLS Oct 09 '14 at 05:39
  • I guess you are skipping the basics in c#, you can't have property names starting with numbers. – KyorCode Oct 09 '14 at 05:41
  • Perhaps you can clarify more. I understand totally property names aren't supposed to start with numbers. But that's the JSON string given and I guess I am trying to understand how to parse it. – DLS Oct 09 '14 at 05:47
  • What is the result you expect? An array of those transaction objects or an object where the keys are the properties? – KyorCode Oct 09 '14 at 05:50
  • I added more details in the description above to make my question clearer. Basically, I would like an array of transaction objects. – DLS Oct 09 '14 at 05:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62722/discussion-between-kyorcode-and-dhana). – KyorCode Oct 09 '14 at 05:56

3 Answers3

2

It can be done with the JObject in JSON.Net library.

var transactions = JObject.Parse(json).PropertyValues()
                                      .Select(o => o.ToObject<Transaction>());

This should do the trick.

KyorCode
  • 1,477
  • 1
  • 22
  • 32
0

Using the JavaScriptSerializer, you can handle this by deserializing into a Dictionary<string, Transaction>. From there you can convert it to an array pretty easily if you need to.

Example code:

string json = @"
{
    ""2"": {
        ""id"": ""2"",
        ""user_id"": ""59"",
        ""offer_id"": ""1234""
    },
    ""3"": {
        ""id"": ""3"",
        ""user_id"": ""59"",
        ""offer_id"": ""1234""
    }
}";

var serializer = new JavaScriptSerializer();
var dict = serializer.Deserialize<Dictionary<string, Transaction>>(json);
var transactions = dict.Select(kvp => kvp.Value).ToArray();

foreach (Transaction t in transactions)
{
    Console.WriteLine(string.Format(
        "id: {0}, user_id: {1}, offer_id: {2}", t.id, t.user_id, t.offer_id));
}
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
0

I ended up that serializing key-value pairs from Javascript to C# is impossible.

I've changed my code and and made a normal array of objects in JS and sent it to the server.

BUT it is possible to parse a key-value pairs form string variable to Dictionary in C# or to a list of KeyValuePair<x,x>

And it is also possible to build a key-value pair using C# and covert it to JSON or JS

Adel Mourad
  • 1,351
  • 16
  • 13