2011-09-30 15 views
5

Ciao Ho un formato dati JSON qualcuno può aiutarmi a fare oggetto JSONStringer dinamica di questo Stringcome generare JsonStringer per questo JSON Data Format?

{"Text":"Hello Simple Text", 
"Files":[{"ContentType":"image/png", 
"Content":"iVBORw0KGgoAAAANSUhEUgAAAR8AAACMCAIAAADKsDpDAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAB3RJTUUH2wYWDzIB3zSYdQAAAAd0RVh0QXV0aG9yAKmuzEgAAAAMdEVYdERlc2NyaXB0aW9uABMJISMAAAAKdEVYdENvcHlyaWdodACsD8w6AAAADnRFWHRDcmVhdGlvbiB0aW1lADX3DwkAAAAJdEVYdFNvZnR3YXJlAF1w/zoAAAALdEVYdERpc2NsYWltZXIAt8C0jwAAAAh0RVh0V2FybmluZwDAG+aHAAAAB3RFWHRTb3VyY2UA9f+D6wAAAAh0RVh0Q29tbWVudAD2zJa/AAAABnRFWHRUaXRsZQCo7tInAAABAElEQVR4nO2de1zUVf7/3+dzmwsMoCgDXgARBO/"}], 
"AuthToken":"XkWQRd65+H+iPtlOoAEYAR0jrzB1o3UV"} 

ho usato

jsonstr = new JSONStringer().object().key("Text") 
          .value(msg).key("Files").array().object().key(
            "ContentType").value("image/png").key(
            "Content").value(enimg) 
          .endObject().endArray().key("AuthToken").value(token) 
          .endObject(); 

ma il server mi sta dando Messaggio di errore in cambio, non accettando i dati.

+2

possibile duplicato di [Errore Durante l'invio di dati Json al server con stringa immagine codificata Base64] (http://stackoverflow.com/questions/7606841/error-while-sending-json-data-to-server-with-base6 4-encoded-image-string) –

+0

+1 per la bandiera corretta. !! @MarkAllison – MKJParekh

risposta

4

in realtà stavo facendo la thing..everything destra era OK .. il problema era con il pacchetto org.json non era accurato con la stringa Base64

sono passato a un'altra libreria e tutto funzionava ..

https://stackoverflow.com/questions/338586/a-better-java-json-library

vedono la domanda di cui sopra per un altro librerie JSON

che era problema con org.json

sono passato a another..and tutto funziona

nesting too deep in JSON... should I switch to XML?

+0

Il primo collegamento non è valido ora. A quale libreria sei passato? –

3

Questo è un modo per fare ciò che si vuole:

// Creating root JSONObject 
JSONObject json = new JSONObject(); 

// Put in it a String field 
json.put("Text", "Hello sample"); 

// Creating a JSONArray 
JSONArray arr = new JSONArray(); 

//Creating the element to populate the array 
JSONObject element = new JSONObject(); 
element.put("ContentType","image/png"); 
element.put("Content","iVBORw0K...gDXgARBO/"); 
// Put it in the array 
arr.put(element); 

// Put the array and other fileds in the root JSONObject 
json.put("Files", arr); 
json.put("AuthToken", "XkWQ...o3UV"); 

// Get the JSON String 
String s = json.toString(); 
// Get formatted and indented JSON String 
String s2 = json.toString(4); 
// 4 is the number of spaces to indent the string