How to read a struct inside another with Json.Net?

2

I need the program to read this information:

  

{"kiseryota": {"id": 15031780, "name": "Kise Ryota", "profileIconId": 1374, "summonerLevel": 30, "revisionDate": 1475089675000}}

I'm doing this: Structure:

public struct SummonerInfo0
{
    public long Id { get; set; }
    public string Name { get; set; }
    public int ProfileIconId { get; set; }
    public long RevisionDate { get; set; }
    public long Level { get; set; }
}

Declaring an object of the structure:

private SummonerInfo0 _summonerInfo0;

Receiving string:

_jsonSummonerInfo0 = e.Result;

Deserializing (I do not know how to spell):

_summonerInfo0 = JsonConvert.DeserializeObject<SummonerInfo0>(_jsonSummonerInfo0);

The problem is that apparently the way I get the information, it looks like a structure inside the other, I tried to put one inside the other but it did not work, then I do not know what to do:     

asked by anonymous 29.09.2016 / 02:13

1 answer

4

This JSON has a key, so create another struct as follows:

public struct Layout
{
    public SummonerInfo0 kiseryota { get; set; }
} 

public struct SummonerInfo0
{
    [Newtonsoft.Json.JsonProperty("id")]
    public long Id { get; set; }

    [Newtonsoft.Json.JsonProperty("name")]
    public string Name { get; set; }

    [Newtonsoft.Json.JsonProperty("profileIconId")]
    public int ProfileIconId { get; set; }

    [Newtonsoft.Json.JsonProperty("revisionDate")]
    public long RevisionDate { get; set; }

    [Newtonsoft.Json.JsonProperty("level")]
    public long Level { get; set; }
}

Using:

string json = "{\"kiseryota\":{\"id\":15031780,\"name\":\"Kise Ryota\",\"profileIconId\":1374,\"summonerLevel\":30,\"revisionDate\":1475089675000}}";
Layout resultado = JsonConvert.DeserializeObject<Layout>(json);
SummonerInfo0 summonerInfo0 = resultado.kiseryota;

Note: in struct SummonerInfo0 has been decorated so that it correctly recognizes properties.

Form reading each token:

string json = "{\"kiseryota\":{\"id\":15031780,\"name\":\"Kise Ryota\",\"profileIconId\":1374,\"summonerLevel\":30,\"revisionDate\":1475089675000}}";
JsonSerializer serializer = new JsonSerializer();
JsonTextReader reader = new JsonTextReader(new StringReader(json));
SummonerInfo0 c = new SummonerInfo0();
while (reader.Read())
{
    if (reader.TokenType == JsonToken.PropertyName)
    {
        reader.Read();
        var res = serializer.Deserialize(reader);
        var jobj = JObject.Parse(res.ToString());
        c.Id = jobj.SelectToken("id") != null ? jobj.SelectToken("id").Value<long>() : 0;
        c.Name = jobj.SelectToken("name") != null ? jobj.SelectToken("name").Value<string>() : string.Empty;
        c.ProfileIconId = jobj.SelectToken("profileIconId") != null ? jobj.SelectToken("profileIconId").Value<int>() : 0;
        c.RevisionDate = jobj.SelectToken("revisionDate") != null ? jobj.SelectToken("revisionDate").Value<long>() : 0;
        c.Level = jobj.SelectToken("level") != null ? jobj.SelectToken("level").Value<long>() : 0;
    }
}
reader.Close();

c; // variavel preenchida!
    
29.09.2016 / 02:47