S
S
Senture2020-11-22 12:22:17
JSON
Senture, 2020-11-22 12:22:17

Simple JSON deserialization in C# fails, why?

Hello.

I have this JSON string:

{"id":"3","companies_name":"{EQ\r\n"}
{"id":"6","companies_name":"testName Company"}
{"id":"7","companies_name":"testName Company324324"}
{"id":"8","companies_name":"testName Company"}
{"id":"9","companies_name":"testName Companydfgdfgf"}
{"id":"10","companies_name":"testName Company"}
{"id":"13","companies_name":"testName Company"}


I'm using Newtonsoft.Json, like this:
Companies restoredCompanies = JsonConvert.DeserializeObject<Companies>(result.message);


If JSON comes with 1 object, everything is OK, if there are several, then an error occurs:
"Additional text encountered after finished reading JSON content: {. Path '', line 1, position 37."

I understand the reason for the error, that I am trying to write several elements into 1 object, but I tried to do it like this:
List<Companies> restoredCompanies = JsonConvert.DeserializeObject<List<Companies>>(result.message);
// и так:

Companies[] restoredCompanies = JsonConvert.DeserializeObject<Companies[]>(result.message);


And here is the Companies class:
class Companies
    {
        public string id { get; set; }
        public string companies_name { get; set; }
    }


But something doesn't work.

I understand that the question is mega stupid and very simple, please do not judge)

PS Thank you all very much!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vasily Bannikov, 2020-11-22
@Senture

I have this JSON string:

Looks like an invalid json.
If you have a list, then it should be
[
  {"id":"3","companies_name":"{EQ\r\n"},
  {"id":"6","companies_name":"testName Company"},
  {"id":"7","companies_name":"testName Company324324"},
  {"id":"8","companies_name":"testName Company"},
  {"id":"9","companies_name":"testName Companydfgdfgf"},
  {"id":"10","companies_name":"testName Company"},
  {"id":"13","companies_name":"testName Company"}
]

Then you have to do
JsonConvert.Deserialize<Companies[]>(str)
class Company {
  [JsonProperty("id")]
  public string Id { get; set; }

  [JsonProperty("companies_name")]
  public string Name { get; set; }
}

UPD: if this is some kind of log stream, for example, then you can use this approach: https://stackoverflow.com/questions/48882653/deser...
But if these are not logs, then it's better to correct the format.

Q
Qualiant, 2020-11-22
@Qualiant

It can throw an error because it failed to deserialize exactly to the specified class.
I do not like code-first, and in general, de-realization into a class. I am parsing json into a dictionary of dictionaries.

System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
Dictionary<string, object> obj = (Dictionary<string, object>)serializer.DeserializeObject(***json***);

B
BasiC2k, 2020-11-22
@BasiC2k

Take a json string (the most complete one) and run it through this service: https://www.jsonutils.com/
As a result, you will have a class that will take into account all the features.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question