7

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.

8
  • 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. Commented Mar 12, 2015 at 2:46
  • 1
    @KenWhite: This is a comment :) Commented Mar 12, 2015 at 2:46
  • @leppie: My point exactly. It certainly isn't a question. Commented Mar 12, 2015 at 2:47
  • The JSON result you are getting is a JSON array which is perfectly legal, I would recommend you to to read this stackoverflow.com/questions/18192357/… Commented Mar 12, 2015 at 2:48
  • I change it for "Is this a correct Json string?" Commented Mar 12, 2015 at 2:49

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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
@Thomas please create the question related to this, or better yet search google, if you are using JsonConvert this answer should be what you search for.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.