0

I have no clue on how to take the information from my JSON file in the webBrowser:

{
    "somename":{
        "id":51252152,
        "name":"somename",
        "profileIconId":531,
        "summonerLevel":30,
        "revisionDate":25235211
    }
}

This is what I have so far: (although the place I'm stuck is with the label_id.Text where I want the id from the JSON file to be.

            Res_Web = webBrowser1.Document.Body.InnerText;

            var jss = new JavaScriptSerializer();
            var d = jss.Deserialize<dynamic>(Res_Web);

            var list = new JavaScriptSerializer().Deserialize<List<dynamic>>(Res_Web);
            label_id.Text = Convert.ToString(d[Name_b]["id"]);

Stacktrace is giving me: StackTrace

The three answers are correct in each their way. Thanks!

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
Colour
  • 69
  • 1
  • 7
  • What is webBrowser1? – Markus Oct 04 '15 at 17:22
  • Name_b is a public static string Name_b; webBrowser1 is the Forms web browser – Colour Oct 04 '15 at 17:23
  • The code looks correct. But why do you need the second-last line? http://procbits.com/2011/04/21/quick-json-serializationdeserialization-in-c – Markus Oct 04 '15 at 17:36
  • I want to extract the values and assign them to the labels in my application @Magu :) – Colour Oct 04 '15 at 17:36
  • When you select the d[Name_b] it won't be just a simple a dictionary. It will be some object that should have property Value or Children or something like that. What lost you is that you're using dynamics which are represented as dictionaries but it's object's properties that are in the dictionary, not its childrens straight away. You need to find which property holds children in your d[Name_b] – jjaskulowski Oct 04 '15 at 17:45

3 Answers3

1

To deserialize Json I have used the Newtonsoft Json see http://www.newtonsoft.com/json.

When you need to deserialize- create an object which matches the structure of you json.

Then to deserialize it is a simple matter of making a call thus

obj = JsonConvert.DeserializeObject<MyClass>(jsonstring);

If you want to get a node of the Json

jsonnode = (JContainer)rd["jsonnode"]["jsonnodechild"]

and the following can be used to traverse the children of a node

foreach (JContainer s in scopes)
{


}

Also I don't know the full specification of your requirement but there is no need to use a WebBrowser control to make a web call - you can use the WebRequest Class

WebRequest req = null;
WebResponse res = null;
StreamReader req = WebRequest.Create(url);
res = req.GetResponse();    
sr = new StreamReader(res.GetResponseStream());
jsonString = sr.ReadToEnd();
Paul S Chapman
  • 832
  • 10
  • 37
1

I have tested your code in a own WinForm Application and it works. I used System.Web.Extensions 4.0

var Res_Web = "{\"somename\":{\"id\":51252152,\"name\":\"somename\",\"profileIconId\":531,\"summonerLevel\":30,\"revisionDate\":25235211}}";

var jss = new JavaScriptSerializer();
var d = jss.Deserialize<dynamic>(Res_Web);

var list = new JavaScriptSerializer().Deserialize<List<dynamic>>(Res_Web);
var labelText = Convert.ToString(d["somename"]["id"]);

Try an other solution from her: Deserialize JSON into C# dynamic object?

Community
  • 1
  • 1
Markus
  • 3,871
  • 3
  • 23
  • 26
1

Here's what I would suggest. First, create a simple class to hold the summoner data from the inner part of the JSON:

public class Summoner
{
    public int id { get; set; }
    public string name { get; set; }
    public int profileIconId { get; set; }
    public long revisionDate { get; set; }
    public int summonerLevel { get; set; }
}

Since the key (e.g. "somename") in the outer part of the JSON probably varies depending on your query, deserialize into a Dictionary<string, Summoner>:

// this would be the JSON string you retrieve from your web request
string json = @"
{
    ""somename"":{
        ""id"":51252152,
        ""name"":""somename"",
        ""profileIconId"":531,
        ""summonerLevel"":30,
        ""revisionDate"":25235211
    }
}";

var jss = new JavaScriptSerializer();
var result = jss.Deserialize<Dictionary<string, Summoner>>(json);

Now you can extract the data by getting the Value of the first key from the dictionary. The value will be an instance of the Summoner class, which you can easily work with:

var summoner = result.First().Value;

Console.WriteLine("id = " + summoner.id);
Console.WriteLine("name = " + summoner.name);
Console.WriteLine("level = " + summoner.summonerLevel);

Output of the above:

id = 51252152
name = somename
level = 30
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300