There is a response from the server like {"name": "Jonh", "lastName": "Smith"}, {"name": "Rick", "lastName": "Grimes"}. Etc. only about 30 objects. Now the implementation is far from ideal, and one request is sent to one object:

var rick = new WebClient().DownloadString("http://127.0.0.1/api/v3/dict/lastname?name=Rick"); 

Then I deserialize, and send to class.

  public class Rick { public string name { get; set; } public string lastName { get; set; } } 

And then calmly use in the code further. How can you simplify this? The current implementation is rather slow.

The server gives all the objects on request http://127.0.0.1/api/v3/dict/lastname

  • 2
    If this is your API - then you and the cards in hand. What prevents to get a list of objects, and not to make a separate request for each object or for each property? - AK
  • @AK no, api is not mine. Address simplified for clarity - ExzoTikFruiT
  • one
    MGM, clear. If I understand the situation correctly, there is little that can be done in this case, just contact the API developers with suggestions to reduce the number of requests. From your words, I conclude that the main loss of time is spent on network communication, it is much more than the json deserialization. Look again at the documentation - are there any other methods to get the data from the server at once, instead of making several requests. It is possible that something can be won due to the parallelization of requests. - AK
  • @AK all objects in a crowd can be obtained through 127.0.0.1/api/v3/dict/lastname , as it wrote in the question. The answer comes: {"name": "Jonh", "lastName": "Smith"}, {"name": "Rick", "lastName": "Grimes"} and 30 more such objects. - ExzoTikFruiT
  • one
    Somewhere there is a reference on this topic from EvgeniyZ, but again I can not find - I wrote in response. - AK

1 answer 1

Probably, you do not come {"name":"Jonh","lastName":"Smith"},{"name":"Rick","lastName":"Grimes"} , but [ {"name":"Jonh","lastName":"Smith"},{"name":"Rick","lastName":"Grimes"}] - otherwise it is an invalid json.

Well, then what is difficult? Single Line Deserialization with Json.Net:

 var data = "[{\"name\":\"Jonh\",\"lastName\":\"Smith\"},{\"name\":\"Rick\",\"lastName\":\"Grimes\"}]"; var deserialized = JsonConvert.DeserializeObject<Rick[]>(data); 

Your result:

enter image description here

How then to address we admit at name = Rick to receive its lastName

 var obj = deserialized.FirstOrDefault(x => string.Equals(x.name, "Rick")); obj.Dump(); obj.lastName.Dump(); 

enter image description here

  • Yes that's right. Staples not posvil. Here I reached it, and how then to drag specific values? When deserializing, it will drive all objects into the class. That was the problem. How then can we contact when name = Rick get it lastName - ExzoTikFruiT
  • one
    @ExzoTikFruiT Without [] server response will be an invalid json. - AK
  • @AK Yes, that's right. Staples not posvil. Here I reached it, and how then to drag specific values? When deserializing, it will drive all objects into the class. That was the problem. How then to address we admit at name = Rick to receive its lastName - ExzoTikFruiT
  • one
    @ExzoTikFruiT For example, using linq. Added in response. - AK
  • 2
    @ExzoTikFruiT Dump() is a third-party linqpad program linqpad , you do not need it, do not pay attention to it - tym32167