Ich möchte eine JSON-Zeichenfolge in Android-Bundle konvertieren. Die Anforderung bestand darin, Parameter direkt vom Server an eine Aktivität als JSON und nicht als Paket zu übergeben. Wie konvertiert man eine JSON-Zeichenfolge in ein Android-Bundle? Bitte geben Sie, wenn möglich, einen abstrakten Code an.JSON in Android-Bundle konvertieren
5
A
Antwort
-2
nur eine schnelle 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);
und dann in B.class ...
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String jsonString = extras.getString(A.JSON_STRING);
12
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;
}
5
Das ist spät, aber vielleicht hilft es, jemanden zu finden dieses Thema anschauen:
/** 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;
}
Verwandte Themen
- 1. JSON in .plist konvertieren
- 2. SQLite in JSON konvertieren
- 3. Konvertieren von JSON in NSArray
- 4. xml in json android konvertieren
- 5. Konvertieren von JSON in XML
- 6. JSON-Struktur in BasicDBObject konvertieren
- 7. Konvertieren von Testclientdaten in JSON
- 8. Lua-Daten in JSON konvertieren
- 9. PHP-Ergebnisarray in JSON konvertieren
- 10. Konvertieren geschachtelten JSON in CSV
- 11. Konvertieren Zeichenfolge in JSON-Objekt
- 12. Swift: Struktur in JSON konvertieren?
- 13. JSON in HTML-Baum konvertieren
- 14. Konvertieren Objekt in JSON Zeichenfolge
- 15. Konvertieren von JSON in node.js
- 16. konvertieren HTML-Liste in JSON
- 17. PHP konvertieren Array in JSON
- 18. Konvertieren von Json
- 19. Javascript/Json konvertieren Datenformat
- 20. wie JSON-String konvertieren
- 21. konvertieren rss zu JSON
- 22. Konvertieren Array zu JSON
- 23. Konvertieren Array zu JSON
- 24. JSON-Zeichenfolge in ein JSON-Objekt in Scala konvertieren
- 25. Konvertieren von Wörterbuch in JSON in Python
- 26. Konvertieren Klassenobjekt in JSON in PHP
- 27. Konvertieren von Zeichenfolge in JSON in Python?
- 28. Newton JSON Json zu Liste konvertieren
- 29. konvertieren JSON in eckige Klammer Format (Die JSON-Struktur)
- 30. konvertieren Bash 'ls' Ausgabe in JSON-Array
Ich meinte JSON Objekt Objekt zu bündeln. – Raj
Dies beantwortet die Frage nicht. – Matthew
antwortet nicht ... – Redwarp