JsonConvert.Import in c# value sometimes array, sometimes string

177 views
Skip to first unread message

Fredrik

unread,
Dec 9, 2009, 5:53:23 AM12/9/09
to Jayrock
Hello

I'm using JsonConvert.Import in c#.Net to import data from another
service. So, I have created classes that are importing fine, but now
and then I get problems with the data I'm importing being
inconsistent. For example, one value is sometimes a string and
sometimes an array.

Example, first array, then string:

compatible: [
"153415-2"
"153415-3"
]

compatible: "153403-2"

If the property in my class for that value is a string I get an error
when an array comes along, if I make the property an array it get's
not value when it's a string.

So, please help me, how should I approach this problem. I mean, It
must be quite common that data is inconsistent on the web, and I'm
sure there must be ways to solve this, but I can't figure out how.

Can I make JsonConvert.Import handle this in some way?

Or is this kind of inconstency not recommended in json, should I ask
my data providers to allways deliver an array even if there is only
one value?

Thanks in advance
Fredrik

Atif Aziz

unread,
Dec 10, 2009, 2:18:25 AM12/10/09
to jay...@googlegroups.com
You can type your property as object and then JsonConvert.Import will import it just fine no matter what comes along. You'll then need to handle the value based on its run-time type. For example:
 
if (obj.compatible is string) {
  /* handle string */
} else if (obj.compatible is IList) {  
  /* handle array */
} else {
  /* either null or something else than array or string */
}
 
Or is this kind of inconstency not recommended in json, should I ask
my data providers to allways deliver an array even if there is only
one value?

This is hard to say because it depends on the purpose and meaning of the value. If it can be interpreted both ways then it's not really inconsistent. You're in the best position to make the call on this. If your data provider is sending back a string in case of a one-element array as a mere optimization then I would really question the value of that.
 
- Atif

--

You received this message because you are subscribed to the Google Groups "Jayrock" group.
To post to this group, send email to jay...@googlegroups.com.
To unsubscribe from this group, send email to jayrock+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jayrock?hl=en.



Fredrik

unread,
Dec 10, 2009, 2:46:00 AM12/10/09
to Jayrock
Aha, i see, that makes sense, thanks.

I started trying out importing as a JsonObject instead of custom
classes, and I kinda liked that.

The structure of the data I'm importing is deep and varied, lots of
arrays and hashes, so the custom classes to cover all the data are
many, and don't differ much from the JsonObject, so I think I'll go
for that.

Regards
/Fredrik
> > jayrock+u...@googlegroups.com<jayrock%2Bunsu...@googlegroups.com >
> > .
Reply all
Reply to author
Forward
0 new messages