0

I want to get a web page (it is a server), on which there's information in json format.

I get the said page and if I check variable called json, I see that it contains correct json text.

The question is: how do I parse this JSON information?

Details: I have a WPF app, where I want to have some info from JSON in a Text Block when I press a button - but not everything but, for example, only one field. Also, I can't copy the code in file in advance because webpage is being updated and I need to load it every other time. This code gets the page and JSON code:

  private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        using (var webClient = new System.Net.WebClient())
        {
            var json = webClient.DownloadString((My_Url));
            Newtonsoft.Json.Linq.JObject o = Newtonsoft.Json.Linq.JObject.Parse(json);
            json = Encoding.UTF8.GetString(webClient.DownloadData(My_Url));
            List1.Text = json.ToString();

        }
    }

This is sample json code:

{ "response": { "status": {"ok", "data": { "30" : {"title":{ "London", "Country": "Britain"}, "24": {"title":"Paris", "Country": "France"} }}

So I'd like to see in my listbox a list of "London, Paris"

David Shepard
  • 181
  • 2
  • 10
  • Can you post a sample of the json you're trying to parse? – D Stanley Aug 02 '17 at 19:04
  • @DStanley yes I will tomorrow, now I can't access this server – David Shepard Aug 02 '17 at 19:06
  • 1
    Looks like you are downloading the same data twice? And if you get a JObject, you can inspect and get data from that. – crashmstr Aug 02 '17 at 19:07
  • @crashmstr I don't know how to get this data – David Shepard Aug 02 '17 at 19:13
  • `List.Text = (string)o["property1"]["childProp"]`? [LINQ to JSON](http://www.newtonsoft.com/json/help/html/LINQtoJSON.htm) (you found enough of this information for get the `JObject.Parse(json)`) – crashmstr Aug 02 '17 at 19:15
  • @crashmstr so in TextBlock I will see only those fields of json which I have enlisted? – David Shepard Aug 02 '17 at 19:19
  • Read through the examples in the link from my previous comment, then if you still have a question, 1. show the exact JSON data 2. the code you have tried. – crashmstr Aug 02 '17 at 19:21
  • @crashmstr if I understood correctly these examples, I should have a json code in advance, but it is loaded from the page. I want this loaded code be transformed into classes automatically like if I would have done using Edit-special paste-paste json as classes – David Shepard Aug 02 '17 at 19:31
  • @NickShepard If the data changes randomly each time you request it, good luck. Otherwise, get it, save it, look at it (and even paste it JSON -> class and deserialize it that way). You *need* to know something about what you are looking for in advance, or you need to be really good at fuzzy searching the data. – crashmstr Aug 02 '17 at 19:39
  • @crashmstr I know how the properties(fields) are called, but content may be different from request to request, So, I need to deserialize my variablr called json? – David Shepard Aug 02 '17 at 19:49
  • @NickShepard if it varies on each request and you can't show me the two or three forms it might take, there is no way *I* can help. You would need to iterate over the deserialized content and try and find what you are looking for. But it sounds like this is a very poor API. – crashmstr Aug 02 '17 at 19:51
  • @crashmstr okay, tomorrow morning I will add to my question sample of json code – David Shepard Aug 02 '17 at 19:52
  • @NickShepard and to clarify: I'm talking about the *structure* and *organization* of the json changing, not the *value* contained within. I would expect the *values* to change over time. If you know the structure, everything is easy (download it, paste json as class, and deserialize to that). – crashmstr Aug 02 '17 at 19:56
  • @crashmstr I've edited my question – David Shepard Aug 03 '17 at 07:22
  • @crashmstr this is the question - how to paste it as class and then deserialize – David Shepard Aug 03 '17 at 07:23
  • @DStanley I've added sample json – David Shepard Aug 03 '17 at 07:27

1 Answers1

-1

I think you want to Deserialize that json.

List1.Text = JsonConvert.DeserializeObject<string>(json);
Alex
  • 1,013
  • 1
  • 13
  • 27
  • Thanks! But I have an exception "unexpected valid encountered while parsing value – David Shepard Aug 03 '17 at 07:37
  • that error is thrown when you don't pass a json to json deserializer, look this: https://stackoverflow.com/questions/23259173/unexpected-character-encountered-while-parsing-value – Alex Aug 03 '17 at 07:42
  • what advice should I use? I tried to implement File.ReadAllText, but then there's an exception that URI is not supported. So now I want listbox to show list of "titles" of my json (London and Paris) – David Shepard Aug 03 '17 at 07:55
  • Firstly, which is the flow? What do you want to do? – Alex Aug 03 '17 at 07:57
  • Answered in previous comment – David Shepard Aug 03 '17 at 07:59
  • Ok, you need to see how looks that json and create a class after that json, after that you need to deserialize with that class or list of that class, depends on json. – Alex Aug 03 '17 at 08:12
  • is of possible to create these classes automatically, not manually? That somehow it is created by itself – David Shepard Aug 03 '17 at 08:16
  • I think no... but you could try something like this: JsonConvert.DeserializeObject>>>(json); – Alex Aug 03 '17 at 08:22
  • exception:cannot deserialize current json object into type system.collections.generic – David Shepard Aug 03 '17 at 08:26
  • okay I created class Cities with property title, what should I do to enlist cities in listbox? – David Shepard Aug 03 '17 at 08:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150917/discussion-between-alex-and-nick-shepard). – Alex Aug 03 '17 at 08:34