java 将 JSON 转换为 Android Bundle
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28984789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert JSON to Android Bundle
提问by Raj
I want to convert a JSON string to android Bundle. The requirement was like to pass parameters to an activity directly from server as JSON rather than bundle. How to convert a JSON string to Android Bundle? Please provide abstract code if possible.
我想将 JSON 字符串转换为 android Bundle。需求就像将参数作为 JSON 而不是捆绑直接从服务器传递给活动。如何将 JSON 字符串转换为 Android Bundle?如果可能,请提供抽象代码。
采纳答案by Matej ?pilár
just a quick SSCCEE
只是一个快速的SSCCEE
A.class
一类
// key for bundle ...
public static final JSON_STRING = "jsonString";
Intent intent = new Intent(A.this, B.class);
Bundle bundle = new Bundle();
bundle.putString(JSON_STRING,json.toString());
intent.putExtras(bundle);
startActivity(intent);
and then in B.class...
然后在B.class...
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String jsonString = extras.getString(A.JSON_STRING);
回答by Nick
public static Bundle jsonStringToBundle(String jsonString){
try {
JSONObject jsonObject = toJsonObject(jsonString);
return jsonToBundle(jsonObject);
} catch (JSONException ignored) {
}
return null;
}
public static JSONObject toJsonObject(String jsonString) throws JSONException {
return new JSONObject(jsonString);
}
public static Bundle jsonToBundle(JSONObject jsonObject) throws JSONException {
Bundle bundle = new Bundle();
Iterator iter = jsonObject.keys();
while(iter.hasNext()){
String key = (String)iter.next();
String value = jsonObject.getString(key);
bundle.putString(key,value);
}
return bundle;
}
回答by pscholl
This is late, but maybe it helps someone finding this thread:
这很晚了,但也许它可以帮助某人找到此线程:
/** Convert a JSON object to a Bundle that can be passed as the extras of
* an Intent. It passes each number as a double, and everything else as a
* String, arrays of those two are also supported. */
public static Bundle fromJson(JSONObject s) {
Bundle bundle = new Bundle();
for (Iterator<String> it = s.keys(); it.hasNext(); ) {
String key = it.next();
JSONArray arr = s.optJSONArray(key);
Double num = s.optDouble(key);
String str = s.optString(key);
if (arr != null && arr.length() <= 0)
bundle.putStringArray(key, new String[]{});
else if (arr != null && !Double.isNaN(arr.optDouble(0))) {
double[] newarr = new double[arr.length()];
for (int i=0; i<arr.length(); i++)
newarr[i] = arr.optDouble(i);
bundle.putDoubleArray(key, newarr);
}
else if (arr != null && arr.optString(0) != null) {
String[] newarr = new String[arr.length()];
for (int i=0; i<arr.length(); i++)
newarr[i] = arr.optString(i);
bundle.putStringArray(key, newarr);
}
else if (!num.isNaN())
bundle.putDouble(key, num);
else if (str != null)
bundle.putString(key, str);
else
System.err.println("unable to transform json to bundle " + key);
}
return bundle;
}
回答by jcady
jaffa's answer is good, but it only works for depth=1 JSON objects. I improved on it by adding support for nested objects.
jaffa 的回答很好,但它只适用于 depth=1 JSON 对象。我通过添加对嵌套对象的支持对其进行了改进。
private static Bundle jsonStringToBundle(String jsonString) {
try {
JSONObject jsonObject = new JSONObject(jsonString);
return jsonToBundle(jsonObject);
} catch (JSONException ignored) {}
return null;
}
private static Bundle jsonToBundle(JSONObject jsonObject) throws JSONException {
Bundle bundle = new Bundle();
Iterator iter = jsonObject.keys();
while (iter.hasNext()) {
String key = (String)iter.next();
String value = jsonObject.getString(key);
Bundle bundleVal = jsonStringToBundle(value);
if (bundleVal != null) {
bundle.putBundle(key, bundleVal);
} else {
bundle.putString(key, value);
}
}
return bundle;
}
Usage:
用法:
Bundle bundle = jsonStringToBundle("{...}");