[英]How to convert int Array to JSON String in Android?
我希望能找到這個問題,但是我找不到。 也許我在搜索錯誤的內容。
我有一個原始整數數組( int[]
),我想將其轉換為String
,即“ JSON-Parseable”,以將其轉換回相同的int[]
。
我嘗試了這段代碼:
// int[] image_ids_primitive = ...
JSONArray mJSONArray = new JSONArray(Arrays.asList(image_ids_primitive));
String jSONString = mJSONArray.toString();
Prefs.init(getApplicationContext());
Prefs.addStringProperty("active_project_image_ids", jSONString);
// Note: Prefs is a nice Class found in StackOverflow, that works properly.
當我打印jSONString
變量時,它的值為: ["[I@40558d08"]
相反,我期望這樣的正確JSON字符串: {"1" : "424242" , "2":"434343"}
不確定引號,但是您知道了。
我想跟蹤本地圖像(在drawable文件夾中),因此我將其ID存儲在int數組中,然后以JSON字符串的形式存儲此數組,稍后將由另一個活動對其進行解析。 而且我知道我可以通過Intent Extras實現此目的。 但是我必須使用SharedPreferences來做。
謝謝你的幫助!
您不必使用Arrays.asList實例化JSONArray。 它可以采用普通的原始數組。
嘗試JSONArray mJSONArray = new JSONArray(image_ids_primitive);
如果您使用的API級別低於19,一種簡單的方法就是遍歷int數組並將其放入。
JSONArray mJSONArray = new JSONArray();
for(int value : image_ids_primitive)
{
mJSONArray.put(value);
}
資料來源: Android開發人員文件
試試這個
int [] arr = {12131,234234,234234,234234,2342432};
JSONObject jsonObj = new JSONObject();
for (int i = 0; i < arr.length; i++) {
try {
jsonObj.put(""+(i+1), ""+arr[1]);
} catch (Exception e) {
}
}
System.out.println("JsonString : " + jsonObj.toString());
// If you wants the data in the format of array use JSONArray.
JSONArray jsonarray = new JSONArray();
//[1,2,1,] etc..
for (int i = 0; i < data.length; i++) {
jsonarray.put(data[i]);
}
System.out.println("Prints the Json Object data :"+jsonarray.toString());
JSONObject jsonObject=new JSONObject();
// If you want the data in key value pairs use json object.
// i.e {"1":"254"} etc..
for(int i=0;i<data.length;i++){
try {
jsonObject.put(""+i, data[i]);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("Prints the Json Object data :"+jsonObject.toString());
如果您想要一個JSON數組而不一定是一個對象,則可以使用JSONArray
。
或者,要快速破解,請執行以下操作:
System.out.println(java.util.Arrays.toString(new int[]{1,2,3,4,5,6,7}));
打印出來
[1, 2, 3, 4, 5, 6, 7]
這是有效的JSON。 如果您想要比這更復雜的事情,顯然JSONObject
是您的朋友。
itertate this through the int array and you will get the json string
JSONStringer img = null ;
img = new JSONStringer() .object() .key("ObjectNAme")
.object() .key("something").value(yourIntarrayAtIndex0)
.key("something").value(yourIntarrayAtIndex1) .key("something").value(yourIntarrayAtIndex2)
.key("something").value(yourIntarrayAtIndex3)
.endObject() .endObject();
String se = img.toString();
Here se is your json string in string format
這段代碼將實現您追求的目標...
int index = 0;
final int[] array = { 100, 200, 203, 4578 };
final JSONObject jsonObject = new JSONObject();
try {
for (int i : array) {
jsonObject.put(String.valueOf(index), String.valueOf(i));
index++;
}
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("YOUR_TAG", jsonObject.toString());
這將為您提供{“ 3”:“ 203”,“ 2”:“ 200”,“ 1”:“ 100”,“ 4”:“ 4578”}作為字符串。
不能完全確定為什么它的順序不正確,但是按鍵排序很容易。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.