0

I'm using Windows Phone 8.1 SDK. I'm trying to convert a JSON string into a dynamic object. This is different from most cases because, just like with the Facebook APIs, there's no predefined class to associate the string with. Specifically, I have a json string like:

{
    "indexes": {
        "000000": "3d6d0abf0ae645eaf8bf090a2685c29a",
        "000001": "3d6d0abf0ae645eaf8bf090a2685c29a"
    }
}

and so on. This means I can't obviously associate a class to the object, because property names are dynamic. What I want is being able to iterate through the values, keeping in mind that the hierarchy is "indexes"->"000000"->Value , "indexes"->"000001"->Value , "indexes"->"..."->Value . I've looked into JSON.NET , trying to deserialize into an ExpandoObject, but that doesn't work because it looks like the ExpandoObjectConverter gives a bunch of compilation errors, maybe because of the Windows Phone 8.1 environment? Anyway, I've kinda hit a wall, so any suggestions would be welcome.

EDIT: My example was poorly chosen, what I need is a more general-purpose conversion, because it could be a recursive structure, where one or more fields may be missing, e.g.:

{
    "friends": {
        "020709": {
               "JohnSmith" : {
                               "email": "johnsmith@something",
                               "mobile": "110011001100"
                             }
                  },
        "010305": {
               "PaulRoss" : {
                               "address": "Some way or the other",
                               "email": "paulross@something",
                             }
                  }
               }}

This is quite easy to do in Perl because of generic hash maps, but it looks like there is no real equivalent in C#?

ChatterOne
  • 3,381
  • 1
  • 18
  • 24
  • Take a look at this: http://stackoverflow.com/questions/24536533/how-can-i-parse-a-json-string-that-would-cause-illegal-c-sharp-identifiers/24536564 – Ilija Dimov Nov 09 '14 at 18:02
  • @IlijaDimov : Thank you, that is almost what I need but not quite. I have edited my question to be more specific. – ChatterOne Nov 09 '14 at 21:44

1 Answers1

0

If your data is highly variable and you don't want to create a rigid class structure, then your best bet is to deserialize to a JToken, then use the LINQ-to-JSON API to extract the data you need from it. Here's an example:

string json = @"
{
    ""friends"": {
        ""020709"": {
            ""JohnSmith"": {
                ""email"": ""johnsmith@something"",
                ""mobile"": ""110011001100""
            }
        },
        ""010305"": {
            ""PaulRoss"": {
                ""address"": ""Some way or the other"",
                ""email"": ""paulross@something""
            }
        }
    }
}";

JToken root = JToken.Parse(json);
foreach (JProperty idProp in root["friends"])
{
    foreach (JProperty nameProp in idProp.Value)
    {
        JToken details = nameProp.Value;
        Console.WriteLine("id: " + idProp.Name);
        Console.WriteLine("name: " + nameProp.Name);
        Console.WriteLine("address: " + (string)details["address"]);
        Console.WriteLine("email: " + (string)details["email"]);
        Console.WriteLine("mobile: " + (string)details["mobile"]);
        Console.WriteLine();
    }
}

Output:

id: 020709
name: JohnSmith
address:
email: johnsmith@something
mobile: 110011001100

id: 010305
name: PaulRoss
address: Some way or the other
email: paulross@something
mobile:
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300