2012-05-11 20 views
7

Tentativo di comprendere il JSON in Delphi. Utilizzando il modulo "DBXJSON.pas". Come usarlo per rendere questo una tale varietà:Delphi: array JSON

Array:[ 
     {"1":1_1,"1_2_1":1_2_2}, 
     ..., 
    ] 

questo modo:

JSONObject:=TJSONObject.Create; 
JSONArray:=TJSONArray.Create(); 
... 
JSONArray.AddElement(TJSONObject.Create(TJSONPair.Create('1','1_1'))); 
JSONArray.AddElement(TJSONObject.Create(TJSONPair.Create('1_2_1','1_2_2'))); 
JSONObject.AddPair('Array',JSONArray); 

, ma arrivare a questo:

{ 
"Array":[ 
{"1":"1_1"},{"1_2_1":"1_2_2"} 
] 
} 

Please help! Grazie!

+1

Ci sono indubbi vantaggi nell'usare qualsiasi cosa venga fornita con Delphi, ma se hai intenzione di fare un sacco di cose JSON, dovresti dare un'occhiata a SuperObject. È fantastico in tanti modi. http://code.google.com/p/superobject/source/checkout –

+0

Si dichiara di volere un valore nell'oggetto di '1_1'. Questo non è un valore Javascript valido. I valori letterali numerici non possono contenere caratteri di sottolineatura. (Sono permessi in Java e Perl, ma non in Javascript.) Si prega di chiarire cosa si voleva veramente, perché al momento la tua domanda appare, quello che vuoi non è affatto JSON, quindi non dovresti usare una libreria JSON. –

risposta

10

Codice, che hai postato sopra, non è corretto. Hai creato una matrice JSON e stai provando ad aggiungere elementi di coppia a quell'array. Ma, invece di aggiungere coppie all'array, devi aggiungere JSON Objects a questo array e questi oggetti devono contenere le tue coppie.
qui è un codice di esempio per risolvere il problema:

program Project3; 

{$APPTYPE CONSOLE} 

uses 
    SysUtils, dbxjson; 

var jsobj, jso : TJsonObject; 
    jsa : TJsonArray; 
    jsp : TJsonPair; 
begin 
    try 
    //create top-level object 
    jsObj := TJsonObject.Create(); 
    //create an json-array 
    jsa := TJsonArray.Create(); 
    //add array to object 
    jsp := TJSONPair.Create('Array', jsa); 
    jsObj.AddPair(jsp); 

    //add items to the _first_ elemet of array 
    jso := TJsonObject.Create(); 
    //add object pairs 
    jso.AddPair(TJsonPair.Create('1', '1_1')); 
    jso.AddPair(TJsonPair.Create('1_2_1', '1_2_2')); 
    //put it into array 
    jsa.AddElement(jso); 

    //second element 
    jso := TJsonObject.Create(); 
    //add object pairs 
    jso.AddPair(TJsonPair.Create('x', 'x_x')); 
    jso.AddPair(TJsonPair.Create('x_y_x', 'x_y_y')); 
    //put it into array 
    jsa.AddElement(jso); 

    writeln(jsObj.ToString); 
    readln; 

    except 
    on E: Exception do 
     Writeln(E.ClassName, ': ', E.Message); 
    end; 
end. 

ed uscita è

{"Array":[ 
    {"1":"1_1","1_2_1":"1_2_2"}, 
    {"x":"x_x","x_y_x":"x_y_y"} 
    ] 
} 
0

Stessa risposta come @teran:

cambiamento:

JSONArray.AddElement(TJSONObject.Create(TJSONPair.Create('1','1_1'))); 
JSONArray.AddElement(TJSONObject.Create(TJSONPair.Create('1_2_1','1_2_2'))); 

a:

JSONArray.AddElement(TJSONPair.Create('1','1_1')); 
JSONArray.AddElement(TJSONPair.Create('1_2_1','1_2_2')); 

Cheers.

+0

Il metodo 'AddElement' si aspetta un' TJSONValue', ma 'TJSONPair' non è una sottoclasse di' TJSONValue', quindi il codice suggerito non verrà nemmeno compilato. L'output desiderato è che l'array contenga un valore. Stai cercando di aggiungere coppie direttamente alla matrice, ma le coppie non sono valori. Quindi, in che modo la tua risposta è la stessa di Teran? –

+0

@Rob Kennedy Ok, ho ricevuto un errore, l'idea era rimuovere l'oggetto di livello extra – umlcat