Json with C # with numbered node

1

Does anyone know how I can read objects with this file template? It looks like the creator did not use [] to Arrays and also the second node is numbered (not key: value as json base).

{  
    "1":{  
        "id":1,
        "nome":"Rodrigo",
        "Apelido":"RK",
        "Fotos":{  
            "120x120":"nome",
            "60x60":"nome",
            "30x30":"nome"
        }
    },
    "2":{  
        "id":1,
        "nome":"Renato",
        "Apelido":"RT",
        "Fotos":{  
            "120x120":"nome",
            "60x60":"nome",
            "30x30":"nome"
        }
    },
    "3":{  
        "id":1,
        "nome":"Luis",
        "Apelido":"LP",
        "Fotos":{  
            "120x120":"nome",
            "60x60":"nome",
            "30x30":"nome"
        }
    }
}
    
asked by anonymous 27.07.2017 / 22:47

2 answers

2

I would advise creating two classes

public class Items: Dictionary<string, Item>
{

}

public class Item
{
    [Newtonsoft.Json.JsonProperty("id")]
    public int Id{ get; set; }
    [Newtonsoft.Json.JsonProperty("nome")]
    public string  Nome { get; set; }
    [Newtonsoft.Json.JsonProperty("Apelido")]
    public string Apelido { get; set; }    
    [Newtonsoft.Json.JsonProperty("Fotos")]        
    public Dictionary<string, string> Fotos { get; set; }
}

and with a simple command using the Json.NET library, do the following:

string json = System.IO.File.ReadAllText("./base.json");

Items objectJson = Newtonsoft.Json.JsonConvert.DeserializeObject<Items>(json);

This variable objectJson is now a type of class Items it's just to access the keys and value easily to search the information contained in the json file.

    
27.07.2017 / 23:03
0

Actually in json is always the combination chave:valor

Note that the node you say is numbered is a string: "1":{

The form of access is the default:

string id = Convert.ToString(jsonObj["1"]["id"]);
string nome = Convert.ToString(jsonObj["1"]["nome"]);
    
27.07.2017 / 22:53