C # - analizza i dati formattati json in hashtable nidificati
Domanda
Sto provando a lavorare con alcuni dati in formato json in C #, ma sto riscontrando dei problemi nel determinare il modo corretto di affrontare il problema. Il mio problema è che i dati in formato json saranno in un formato sconosciuto (so che sembra strano & # 8230; continua a leggere). Fondamentalmente, i dati in formato json saranno una raccolta di coppie nome / valore in cui i valori possono o meno essere matrici di coppie nome / valore nidificate. Per rendere le cose più divertenti, la nidificazione delle matrici di coppie nome / valore può continuare all'infinito.
Ad esempio: Potrei avere alcuni dati che sembrano & # 8230;
{
"1": {
"1.1": {
"1.1.1": "value1",
"1.1.2": "value2",
"1.1.3": "value3"
},
"1.2": "value4",
"1.3": {
"1.3.1": {
"1.3.1.1": "value5",
"1.3.1.2": "value6"
},
"1.3.1.2": "value7",
"1.3.1.3": "value8"
}
}
}
Sfortunatamente, non so quanto si verificherà l'annidamento e tecnicamente non so quali nomi / coppie di valori saranno presenti in un determinato messaggio.
Esiste un meccanismo supportato in C # che mi consentirebbe di analizzarlo facilmente in un set nidificato di hastable?
Mi piacerebbe fare qualcosa sulla falsariga di (nota che questo codice non è sintatticamente corretto al 100% e sarebbe meglio farlo tramite ricorsione & # 8230; ma otterrà l'idea attraverso).
Hashtable ht = [deserialize data method](jsonformattedstring);
foreach (Hashtable nested in ht)
{
If (nested.count > 1)
{
Foreach (hashtable next in nested)
…
}
}
Soluzione
Non mi è piaciuto il .Net Json che analizza ... ogni tanto fa delle strane cose. Sono passato a Json.NET , una libreria open source. Ha un bel oggetto JObject che farà ciò di cui hai bisogno.
Altri suggerimenti
In .NET, hai JsonArray , che consente di caricare e analizzare i dati JSON. Crea una matrice di JsonValue ed è completamente nidificato in base ai dati JSON che analizza.
Se hai bisogno specificamente di Hashtable, potresti tradurre i dati da JsonArray, sebbene Hastable sia quasi completamente deprecato a favore del dizionario.
Josh Holmes ha un buon "inizio" " pubblica un post su JSON in .NET: http://www.joshholmes.com/blog/2009/01/ 20 / PlayingWithJSON.aspx
Potresti voler guardare http://techblog.procurios.nl/k/n618/news/view/14605/14863/How-do-I-write-my-own-parser-for- JSON.html È una semplice libreria che analizza una stringa JSON in Hashtables e ArrayLists. Può anche trasformare di nuovo queste strutture in JSON.
Ecco un metodo che ho scritto in C # per analizzare JSON e restituire un dizionario. Ovviamente non è appropriato per tutti i casi d'uso, ma qualcosa del genere ti darà una buona analisi one-pass del JSON:
/*
* This method takes in JSON in the form returned by javascript's
* JSON.stringify(Object) and returns a string->string dictionary.
* This method may be of use when the format of the json is unknown.
* You can modify the delimiters, etc pretty easily in the source
* (sorry I didn't abstract it--I have a very specific use).
*/
public static Dictionary<string, string> jsonParse(string rawjson)
{
Dictionary<string, string> outdict = new Dictionary<string, string>();
StringBuilder keybufferbuilder = new StringBuilder();
StringBuilder valuebufferbuilder = new StringBuilder();
StringReader bufferreader = new StringReader(rawjson);
int s = 0;
bool reading = false;
bool inside_string = false;
bool reading_value = false;
//break at end (returns -1)
while (s >= 0)
{
s = bufferreader.Read();
//opening of json
if (!reading)
{
if ((char)s == '{' && !inside_string && !reading) reading = true;
continue;
}
else
{
//if we find a quote and we are not yet inside a string, advance and get inside
if (!inside_string)
{
//read past the quote
if ((char)s == '\"') inside_string = true;
continue;
}
if (inside_string)
{
//if we reached the end of the string
if ((char)s == '\"')
{
inside_string = false;
s = bufferreader.Read(); //advance pointer
if ((char)s == ':')
{
reading_value = true;
continue;
}
if (reading_value && (char)s == ',')
{
//we know we just ended the line, so put itin our dictionary
if (!outdict.ContainsKey(keybufferbuilder.ToString())) outdict.Add(keybufferbuilder.ToString(), valuebufferbuilder.ToString());
//and clear the buffers
keybufferbuilder.Clear();
valuebufferbuilder.Clear();
reading_value = false;
}
if (reading_value && (char)s == '}')
{
//we know we just ended the line, so put itin our dictionary
if (!outdict.ContainsKey(keybufferbuilder.ToString())) outdict.Add(keybufferbuilder.ToString(), valuebufferbuilder.ToString());
//and clear the buffers
keybufferbuilder.Clear();
valuebufferbuilder.Clear();
reading_value = false;
reading = false;
break;
}
}
else
{
if (reading_value)
{
valuebufferbuilder.Append((char)s);
continue;
}
else
{
keybufferbuilder.Append((char)s);
continue;
}
}
}
else
{
switch ((char)s)
{
case ':':
reading_value = true;
break;
default:
if (reading_value)
{
valuebufferbuilder.Append((char)s);
}
else
{
keybufferbuilder.Append((char)s);
}
break;
}
}
}
}
return outdict;
}