Good day. I'm still new to C Sharp, so the next question may seem trivial. In general, I make a GET request to a certain URL, for example /version.html. In response, I receive this information:

application.VERSION = { VERSION: "194", BUILD: "19758" }; 

Actually, the question. Is it possible to convert this string to an array? How to read data pairs "constant: value"?

  • In C #, the line is immediately split into an array. - darkwoolf
  • The fact is that I read the server's response as follows string HTMLoutput = readStream.ReadToEnd (); those. get the string. And from it you need to get data in the form of an array - Hicks
  • Describe exactly how you want to see the array? Array what do you want to see? And why aren't you satisfied with the char array? When parsing the answer, it is better to use the HttpWebResponse object. - Yury Bakharev
  • Maybe I incorrectly formulated the question. The task is to get the VERSION and BUILD values ​​from the plain-text server response (the answer itself in the question) to compare the relevance of the version. What can be done with a similar response from the server? - Hicks
  • 3
    Colleagues, take a look. This is not JSON :) - eastwing

3 answers 3

This is your JSON . So use any library to work with JSON, which one you like. Something like this might look like a solution on Json.NET

 using System; using System.Text.RegularExpressions; using Newtonsoft.Json.Linq; namespace Test { static class Program { private static void Main() { string input = "application.VERSION = { VERSION: \"194\", BUILD: \"19758\" };"; // Обрезаем всё, что за пределами фигурных скобок, иначе Json.NET не съест строку. input = Regex.Match(input, "{.*}").Value; // Создаем объектовую модель из строки. JObject jo = JObject.Parse(input); // Теперь можно обращаться к полям JSON-объекта по именам. Console.WriteLine("Version = " + jo["VERSION"]); Console.WriteLine("Build = " + jo["BUILD"]); } } } 
  • 2
    As @eastwing correctly noted in the comments to the question, we are not dealing entirely with JSON. We do not have the keys here in quotes. So strictly speaking, my answer is incorrect. But for some reason, Json.NET successfully completed the proposed string, and the sample code works. Probably, I will not delete the answer yet. Maybe someone will comment on why everything worked. Perhaps quotes are optional, for example. - player one
  • one
    JavaScriptSerializer also successfully eats a string after trimming. Although yes, the keys must be in quotes. - Alexander Petrov
  • one
    Yes confirm. even without quotes in the keys - it works. The only thing that does not want to work, if there are line breaks, so I had to do input = input.Replace ("\ n", ""); before calling Regex.Match - Hicks
  • 2
    @Hicks: Json.NET has a mode when it does not require quotes (this is configurable). But the replacement \n should not be needed. - VladD

Although the text above is similar to JSON, it is not.

Alternatively, you can use a regular expression.

 string input = "application.VERSION = { VERSION: \"194\", BUILD: \"19758\" };"; string pattern = @"(\w+) : \s+ "" (.+?) "" "; var matches = Regex.Matches(input, pattern, RegexOptions.IgnorePatternWhitespace); foreach (Match m in matches) { Console.WriteLine("Name: " + m.Groups[1].Value); Console.WriteLine("Value: " + m.Groups[2].Value); } 
  • also a working version. But for some reason does not want to work out the values ​​at which there are points, for example. string input = "application.VERSION = {VERSION: \" 1.9.4 \ ", BUILD: \" 19758 \ "};"; it won't work anymore - Hicks
  • one
    @Hicks - corrected the regulars. Now with points will work. - Alexander Petrov

There are several options for working with JSON in C #. Here it is well described https://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c . In short, Json.NET http://www.newtonsoft.com/json is good for these tasks. Here is an example from their site how to deserialize JSON (true to a class, not to an array). Naturally there should be a class with fields with the same names.

 string json = @"{ 'Name': 'Bad Boys', 'ReleaseDate': '1995-4-7T00:00:00', 'Genres': [ 'Action', 'Comedy' ] }"; Movie m = JsonConvert.DeserializeObject<Movie>(json); string name = m.Name; 
  • It is worth adding the main answer from the link - Grundy
  • Grundy, in my opinion something like this) - Vlad Bayrak