2011-09-30 65 views
5

你好,我有一個JSON數據格式可以請人幫我做動態JSONStringer對象此字符串如何爲Json數據格式生成JsonStringer?

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

我已經使用

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

但服務器是給我的回報故障信息,不接受數據。

+2

可能重複[使用Base64編碼圖像字符串將Json數據發送到服務器時出現錯誤](http://stackoverflow.com/questions/7606841/error-while-sending-json-data-to-server-with-base6 4-encoded-image-string) –

+0

+1爲正確的標誌。 @MarkAllison – MKJParekh

回答

3

這是一種方式做你想要什麼:

// 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