0

I am trying to deserialize a JSON message using Newtonsoft.Json but one of his attributes has a space and returns null as result.

My code to deserialize the message:

JavaScriptSerializer jss;
jss = new JavaScriptSerializer();

using (reader = new StreamReader(stream))
{
    string message = reader.ReadToEnd();

    JsonError jsonError = new JsonError();
    jsonError = jss.Deserialize<JsonError>(message);

    btxErrorMemo.Visible = true;
    btxErrorMemo.Text = errorMessage;
    btxStatusLbl.Text = "ERROR";
}

I show that you have to specify Json attributes to class when the message contains spaces

JsonError class

public class JsonError
{
        [JsonProperty("error")]   //this one works
        public string error { get; set; }
        public string description { get; set; }
        [JsonProperty("error message")]  //this one doesnt work
        public IList<string> errorMessage { get; set; }
}

The JSON message is like this

{
    "error":"xml data validation ",
    "error message": ["\u03a4\u03bf \u03c0\u03b5\u03b4\u03af\u03bf MesIdeMES19 \u03c0\u03c1\u03ad\u03c0\u03b5\u03b9 \u03bd\u03b1 \u03b5\u03af\u03bd\u03b1\u03b9 \u03bc\u03bf\u03bd\u03b1\u03b4\u03af\u03ba\u03bf\u03c2 \u03b1\u03c1\u03b9\u03b8\u03bc\u03cc\u03c2.(\u03a0\u03c1\u03bf\u03c4\u03b5\u03af\u03bd\u03b5\u03c4\u03b1\u03b9 \u03bd\u03b1 \u03b5\u03af\u03bd\u03b1\u03af \u03bf \u03b1\u03c1\u03b9\u03b8\u03bc\u03cc\u03c2 VAT \u03c4\u03b7\u03c2 \u03b5\u03c4\u03b1\u03b9\u03c1\u03b5\u03af\u03b1\u03c2 \u03b7 \u03c3\u03b7\u03bc\u03b5\u03c1\u03b9\u03bd\u03ae \u0397\u03bc\u03b5\u03c1\u03bf\u03bc\u03b7\u03bd\u03af\u03b1. \u03ba\u03b1\u03b9 6 \u03c4\u03c5\u03c7\u03b1\u03af\u03b1 \u03b1\u03bb\u03c6\u03b1\u03c1\u03b1\u03c1\u03b9\u03b8\u03bc\u03b9\u03c4\u03ba\u03ac)","\u03a3\u03c4\u03bf \u03b5\u03bc\u03c0\u03cc\u03c1\u03b5\u03c5\u03bc\u03b1:1 \u03a4\u03bf \u03c0\u03ad\u03b4\u03b9\u03bf (GooDesGDS23) \u03a0\u03b5\u03c1\u03b9\u03b3\u03c1\u03b1\u03c6\u03ae \u03b5\u03af\u03b4\u03bf\u03c5\u03c2 \u03c0\u03c1\u03ad\u03c0\u03b5\u03b9 \u03bd\u03b1 \u03b5\u03af\u03bd\u03b1\u03b9 \u03b1\u03bb\u03c6\u03b1\u03c1\u03b9\u03b8\u03bc\u03b7\u03c4\u03b9\u03ba\u03cc"]
}

I changed the "error message" from

public string errorMessage { get; set; }

to

public IList<string> errorMessage { get; set; }

because I saw that the error message belongs to array and not just string but I keep getting null as result.

3
  • 4
    JsonProperty attribute only works with Newtonsoft.Json Nuget package or json.net serializer, reference how-to-deserialize-json-with-spaces-in-the-attribute-names Commented Apr 5, 2021 at 12:08
  • 2
    You have confused System.Web.Script.Serialization.JavaScriptSerializer and Newtonsoft.Json.JsonSerializer. Commented Apr 5, 2021 at 12:15
  • 1
    As mentioned above you are using an attribute [JsonProperty] from json.net with JavaScriptSerializer which is a completely different serializer. You should use var jsonError = JsonConvert.DeserializeObject<JsonError>(message);. See: JavaScriptSerializer - custom property name. Commented Apr 5, 2021 at 12:48

1 Answer 1

1

Thanks to @Ryan Wilson , @Alexander Petrov and @dbc. The solution was simple. I was using Nettonsoft.Json to declare Json attributes but I was using System.Web.Script.Serialization to deserialize the message. In my case a simple change to code

JavaScriptSerializer jss;    
var jsonError = jss.Deserialize<JsonError>(message);

to

var jsonError = JsonConvert.DeserializeObject<JsonError>(message);

did the trick.

Thank you all for your help.

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

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.