6

I'm getting this result from a web function.

["767,20150221122715,121053103,14573465,1,7,302",
"767,20150221122756,121053165,14573375,1,0,302",
"767,20150221122840,121053498,14572841,1,12,124"]

Usually Json have PropertyName: Value But this have an array of strings, and each string have the values separated by comma. I know what each value position mean.

I try using JsonConvert.DeserializeObject but couldn't make it work.

string deserializedProduct = JsonConvert.DeserializeObject<string>(json);
//and
List<string> deserializedProduct = JsonConvert.DeserializeObject<string>(json);

I can parse the string doing a split, but I'm wondering if there is an easy way.

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118
  • 1
    Can you [edit] the title of your question to something that actually has meaning? *This is a JSON string* has no meaning, and it will be absolutely irrelevant to anyone who finds it in a search result in the future. The title should explain the problem you're having or the question you're asking. Thanks. – Ken White Mar 12 '15 at 02:46
  • 1
    @KenWhite: This is a comment :) – leppie Mar 12 '15 at 02:46
  • @leppie: My point exactly. It certainly isn't a question. – Ken White Mar 12 '15 at 02:47
  • The JSON result you are getting is a JSON array which is perfectly legal, I would recommend you to to read this http://stackoverflow.com/questions/18192357/deserializing-json-object-array-with-json-net – dvhh Mar 12 '15 at 02:48
  • I change it for "Is this a correct Json string?" – Juan Carlos Oropeza Mar 12 '15 at 02:49
  • @JasonZ thanks for the suggestion, As you saw for my first question i wasnt sure that was an Json string. But now you question make it easy to find how to solve it. – Juan Carlos Oropeza Mar 12 '15 at 02:53
  • @JuanCarlosOropeza You're welcome! A good site to use to test out whether you have a valid JSON string or not is http://www.jslint.com/. – Jason Z Mar 12 '15 at 02:55
  • Now with the new title, it really makes it a duplicate of http://stackoverflow.com/a/18192564/105104 – dvhh Mar 12 '15 at 03:03

2 Answers2

4

To answer your question, according to to http://json.org/, it is a valid JSON value (an array of string).

To deserialize it according to this stack overflow question you should use JsonConvert.DeserializeObject<List<string>>(json); to convert it

Community
  • 1
  • 1
dvhh
  • 4,724
  • 27
  • 33
  • Your first assumption was that the JSON result was invaldi which didn't help your search for an answer, I suggest you go upvote the people in the linked SO question which would help people in the future – dvhh Mar 12 '15 at 03:01
  • @Thomas please create the question related to this, or better yet search google, if you are using `JsonConvert` this [answer](http://stackoverflow.com/a/18948852/105104) should be what you search for. – dvhh Jul 19 '16 at 00:58
2

The generic parameter to the DeserializeObject<T>() method is the type you want the deserializer to deserialize to. Your json string represents an array of strings so you should be deserializing to a collection of strings (typically List<string>).

var values = JsonConvert.DeserializeObject<List<string>>(json);

However, it isn't necessary to specify the type. There is a non-generic overload that returns object. It will (in this case) return an instance of a JArray with the appropriate values.

object values = JsonConvert.Deserialize(json);

Though, it would be better to return a more specific type if possible. To keep it more generalized, you can use JToken for the generic type or even more specifically, JArray.

var values = JsonConvert.Deserialize<JToken>(json); // good
var values = JsonConvert.Deserialize<JArray>(json); // better in this case
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272