5

I am writing a program to read a JSON string from Podio, and then convert the contents to c# objects.

But while reading the feed, I came across a strange format; At the same hierarchy-level of object, sometimes the value of the field [value] is a string but other times it is a complex object.

Example is below.

At some places it is like

"values":[
    {
        "value":"Bug on User Interface, Ajax sometimes does not load properly"
    }
],
"type":"text"

and then at the next item, at the same level in the hierarchy, it is like

"values":[
    {
        "value":{
            "perma_link":"https:\/\/ds-test.podio.com\/myworkspace\/files\/23529948",
            "mimetype":"image\/jpeg",
            "hosted_by":"podio",
            "name":"217820_274164679355286_689330144_n.jpg",
            "hosted_by_humanized_name":"Podio",
            "description":null,
            "thumbnail_link":"https:\/\/files.podio.com\/23529948",
            "link":"https:\/\/files.podio.com\/23529948",
            "file_id":23529948,
            "size":39698
        }
    }
],
"type":"image"

Note the value of "type": For the first instance it is "text" and then for the next it is "image". As they are on the same level of the hierarchy, I have no idea how to design the objects for them so that the DataContractJsonSerializer.Read method works smoothly.

Regards,

Guido Preite
  • 14,905
  • 4
  • 36
  • 65
ali zaib
  • 99
  • 8
  • Is `"Bug on User Interface, Ajax sometimes does not load properly"` just an example you've entered to illustrate that it's a string instead of an object? And if so, can you update your question with a few examples of what's _actually_ returned? Or is this string what is really returned? – Sepster Oct 08 '12 at 12:34
  • There are many type returned. e.g image, description etc. so far description value of [value] field has to be string for type image value of [value] field has to be an object. – ali zaib Oct 12 '12 at 12:46
  • Hi Ali, I'd appreciate some feedback on my answer below - especially given it looks as though I was the only person to offer any suggestions, and I gave you quite a comprehensive answer! – Sepster Dec 15 '12 at 03:14

1 Answers1

2

Up front, I'm no JSON expert (I've dabbled), and while I'm a competent C#.net programmer, I've never worked with JSON from .NET. I would've thought you'd have at least one good, definitive answer by now, but since you haven't had any, I'll offer my $0.02 worth, in case it's helpful to you.

This is a valid format, according to http://www.json.org. (I'm not sure if by saying it's a "strange format", you're saying that you think it's not valid, or just that you think it is unusual).

In both example cases you've described, there is a consistent pattern between them:

<string1> : <value1>,
<string2> : <value2>

where in all cases:

  • <string1> == "Values",
  • <string2> == "type",
  • <value1> == [ <object> ] (ie <value1> is always an array containing a single <object>), and
  • <value2> == "a string describing the structure of the <object>"

Also, <object> is either a string when <value2> == "text", or it is a more complex object that contains 1 or more <string> : <value> pairs in a comma separated list when <value2> != "text".

So, how to parse, assuming .Net can't handle this standard format out-of-the-box (I'm surprised it can't)...

  • Build a class definition for each of only the objects you are interested in. You should be able to glean the required structure from the JSON.

  • Have you thought about running a string operation to "find/extract" the types you're interested in, and set up an explicit DataContractJsonSerializer(Type) for each of those types?

  • Otherwise, more generally you should be able to treat everything within <object> when <value2> != "text", as a Dictionary<string,string>, and then process this on a case-by-case basis as required, depending on your type specified at <value2> (or even transform these into your object "manually").


Further, I note that .NET 4.5 introduces this overload:

DataContractJsonSerializer Constructor (Type, DataContractJsonSerializerSettings)

with settings such as:

And particularly interesting:

...a collection of types that may be present in the object graph serialized using this instance the DataContractJsonSerializerSettings.

These may be relevant, but I can't find any more information about any of these settings - perhaps something for you to experiment with.

Sepster
  • 4,800
  • 20
  • 38