10

I currently have a REST app which returns a JSON string something like:

[{error: "Account with that email exists"}]

For when an error is thrown. I don't want to deserialize it into a custom "error" object, because it seems a bit wasteful and pointless. Is there a simple way to just extract a specific field out of a JSON string without making a custom class to reflect it.

Thanks

3
  • 1
    I would definitely deserialize. Any other string search operation would cost you more. Commented May 18, 2016 at 5:38
  • 1
    It would be good if you posted valid json. What you've given us is a segment of valid json, it's incomplete Commented May 18, 2016 at 6:01
  • Possible duplicate of Deserialize json object into dynamic object using Json.net Commented May 18, 2016 at 6:01

3 Answers 3

19

You have a couple of options if you don't want to create a custom class, you can deserialize to dynamic:

dynamic tmp = JsonConvert.DeserializeObject(yourString);
string error = (string)tmp.error;

Or deserialize to a dictionary:

var dic = JsonConvert.DeserializeObject<Dictionary<string, string>>();
string error = dic["error"];
Sign up to request clarification or add additional context in comments.

8 Comments

this is definitely dynamic abuse
@orhor although I don't really agree that deserializing to dynamic when dealing with JSON is "definitely" dynamic abuse, I've posted an alternative.
Why you think it is dynamic abuse? It is avoiding one class to get created just to read error message.
we are in C#, which is a type language and it is its advantage. Why are you against the classes. The best way would be to have, let's say Message type with its Error property and deserialize right into it. I understand that some services might return unsystematic mixed or messed results, but it is different case. In my opinion the proper approach is to keep strongly typed. Sorry for beeing "old-school"
@orhor Ok I've decided to go with creating a subclass which just holds a success or fail message, if it's success then I deserialize the string again but this time into the actual class.
|
8

No need third party libraries. Use native JavaScriptSerializer.

string input = "[{error: \"Account with that email exists\"}]";
var jss = new JavaScriptSerializer();

var array = jss.Deserialize<object[]>(input);
var dict = array[0] as Dictionary<string, object>;
Console.WriteLine(dict["error"]);

// More short with dynamic
dynamic d = jss.DeserializeObject(input);
Console.WriteLine(d[0]["error"]);

2 Comments

Great! I was hoping there would be a native solution
@S.Lukas Json.NET practically is native. There are so many microsoft libraries on github that are dependent on it.
2

Have a look at JObject.

dynamic obj = JObject.Parse("{ myerrors: [{error: \"Account with that email exists\"}] }");
var a = obj.myerrors[0];
string error = a.error;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.