2011-09-30 11 views
5

Witam mam do formatu danych JSON może ktoś proszę mi pomóc, aby obiekt dynamiczny JSONStringer na ten ciągjak wygenerować JsonStringer dla tego formatu danych Json?

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

Użyłem

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(); 

ale serwer daje mi zarzucić wiadomość w zamian nie akceptuje danych.

+2

możliwy duplikat z [Błąd podczas wysyłania danych Json do serwera z zakodowanym łańcuchem obrazów Base64] (http://stackoverflow.com/questions/7606841/error-while-sending-json-data-to-server-with-base6 4-kodowany ciąg znaków) –

+0

+1 dla poprawnej flagi. !! @MarkAllison – MKJParekh

Odpowiedz

4

faktycznie robiłem prawo thing..everything było OK .. Problem polegał na tym, że pakiet org.json był niepoprawny z łańcuchem Base64

przełączyłem się na inną bibliotekę i wszystko działało ..

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

patrz powyższe pytanie na kolejne biblioteki json

że był problem z org.json

przeszedłem do another..and wszystko działa

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

+0

Pierwsze łącze jest teraz nieważne. Do jakiej biblioteki się przełączyłeś? –

3

jest to sposób, aby robić to, co chcesz:

// 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 
Powiązane problemy