I need to get the value from dynamic JSON. It is huge, so bringing it completely does not make sense. I am interested in the values ​​of circulation, how can I get there as gracefully as possible?

Logically, I need to find the word Circulation in a row and go to the next cell and get the value.

enter image description here

  • deserializing a string into an object and accessing a specific field - Grundy
  • I do not want to create classes for this purpose. You can not just find the word circulation and go to the next cell? - shatoidil
  • And the field can not be specific. In another file, the position of the circulation may be a different figure - shatoidil
  • You can not just find the word circulation and go to the next cell - are you sure that you will have only one word "Circulation" in the entire facility? - Grundy
  • And the field can not be specific. In another file, the position of the circulation can be a different figure. Searching in an array of objects using the same linq is much easier than doing a search in the line - Grundy

2 answers 2

Use ready-made parsers. Reading JSON search or regular expressions is not a good idea. For example, the Value property is not required to go to the Name property, it can go after it. The meaning of JSON will not change, and it will be difficult to catch all such cases when searching.

Json.NET supports such a thing as JSONPath queries:

 var json = @"{ ""Editions"": {}, ""ItemType"": 5, ""Capabilities"": { ""Capabilities"": [ { ""Name"": ""Π’ΠΈΡ€Π°ΠΆ"", ""Value"": ""20000"", ""AdditionalProperties"": {} }, { ""Name"": ""aaaa"", ""Value"": ""bbb"", ""AdditionalProperties"": {} } ] } }"; var obj = JObject.Parse(json); var value = (string)obj.SelectToken("$.Capabilities.Capabilities[?(@.Name == 'Π’ΠΈΡ€Π°ΠΆ')].Value"); 

If JSON is really huge and de -serializes its entire memory overhead, you will have to use JsonTextReader , deserializing only small parts of the file (the code is approximate, without checks):

 string value = null; using (var reader = new JsonTextReader(new StringReader(json))) { // Ρ‡ΠΈΡ‚Π°Π΅ΠΌ json, ΠΏΠΎΠΊΠ° Π½Π΅ ΠΏΡ€ΠΈΠ΄Π΅ΠΌ Π² ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ Capabilities while (reader.Read()) { if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "Capabilities") { break; } } reader.Read(); // ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ ΠΈΠ· массива дСсСриализуСм Π² JObject while (reader.Read()) { if (reader.TokenType == JsonToken.StartObject) { var obj = JObject.Load(reader); if ((string)obj["Name"] == "Π’ΠΈΡ€Π°ΠΆ") { value = (string)obj["Value"]; break; } } } } 
  • Outrun me :) - Bulson
  • Correct to var value = (string) json_obj.SelectToken ("$. Capabilities. Capabilities [? (@. Name == 'Circulation')]. Value"); and I will mark as an answer! You got it beautiful! - shatoidil
  • @shatoidil corrected - kmv

Solved the problem as follows.

 JObject json_obj = JObject.Parse(json_data); JArray capabilities = (JArray)json_obj["Capabilities"]["Capabilities"]; var сirculation = capabilities.Where(c => (string)c["Name"] == "Π’ΠΈΡ€Π°ΠΆ").Select(c => c["Value"]).FirstOrDefault();