Voglio convertire una stringa JSON in un pacchetto Android. Il requisito era come passare parametri ad un'attività direttamente dal server come JSON piuttosto che bundle. Come convertire una stringa JSON in un pacchetto Android? Si prega di fornire il codice astratto, se possibile.Converti pacchetto JSON in Android
5
A
risposta
-2
solo un rapido 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);
e poi 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
Questo è in ritardo, ma forse aiuta qualcuno a trovare questa discussione:
/** 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;
}
Problemi correlati
- 1. Converti array in JSON
- 2. Converti array in JSON
- 3. Converti JSON in NSArray
- 4. Converti SQLite in JSON
- 5. Converti JSON in .plist
- 6. Converti JSON in CSV
- 7. Converti array JSON in normale array Java
- 8. Converti oggetto in stringa JSON
- 9. Converti dati Lua in JSON
- 10. Converti BSON in JSON valido
- 11. Converti stringa in oggetto JSON
- 12. Converti JSON per DataTable
- 13. Converti mappa in bundle in android
- 14. Converti JSON String a JSON oggetto C#
- 15. Converti .json in .csv in ruby
- 16. Converti JSON in oggetto classe in C#
- 17. Converti una stringa in JSON in Python?
- 18. Converti json in un array C#?
- 19. Swift: Converti la struttura in JSON?
- 20. Converti la stringa JSON in Lua Table?
- 21. Converti mappa in JSON utilizzando Jackson
- 22. Converti Excel in JSON utilizzando Angularjs
- 23. Converti oggetto di JSON in MVC 4
- 24. Converti dati XML in formato Json AngularJS
- 25. Converti Hashmap in JSON utilizzando GSON
- 26. Converti oggetto di stringa JSON in C#
- 27. Converti oggetto C# in oggetto Json
- 28. Converti dati modulo in oggetto JSON
- 29. Converti Swagger JSON in RAML/YAML
- 30. Converti stringa JSON in lista di oggetti
intendevo JSON oggetto di Bundle oggetto. – Raj
Questo non risponde alla domanda. – Matthew
Non risponde ... – Redwarp