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

Asked

Viewed 96 times

2

I need the program to read this information:

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

Here’s what I’m doing: 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 the string:

_jsonSummonerInfo0 = e.Result;

Deserializing (I don’t know how to spell it):

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

The problem is that apparently the way I get the information, it looks like one structure inside the other, I tried to put one inside the other but it didn’t work, so I don’t know what else to do :/

  • Will pass directly or through parameters?

1 answer

4


That one JSON has a key, so create one more 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;

Observing: in struct SummonerInfo0 has been decorated so that he correctly recognizes the 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!
  • 1

    Hey, in case that first name won’t always be kiseryota, it’s variant, so how to proceed?

  • Do it manual! that if you have any kind of property and the rest of the layout is the same will work

  • Manual you say... Remove this part with a string.Remove?

  • No excuse reading every token of this json.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.