JSONArray à la chaîne en java
J'ai un JSONArray
, ce qui ressemble à ceci:
Comment puis-je convertir cela en chaîne?
Si j'essaie:
json.toString();
La chaîne est:
["package.package.ChecklistModels.ChecklistAnswer@405dddd8","package.package.ChecklistModels.ChecklistAnswer@405ddf48","package.package.ChecklistModels.ChecklistAnswer@405de070","package.package.ChecklistModels.ChecklistAnswer@405de198","package.package.ChecklistModels.ChecklistAnswer@405de2c0","package.package.ChecklistModels.ChecklistAnswer@405de3e8","package.package.ChecklistModels.ChecklistAnswer@405de510"]
Mais je veux quelque chose comme ça:
{
"json":
"values": [
{
"answer": "true",
"remark":"",
"questionId": "0"
"checklistId": "2"
},
{
"answer": "true",
"remark":"",
"questionId": "0"
"checklistId": "2"
}
]
}
MODIFIER:
Ceci a coupé comment je fais le tableau json:
if(cb.isChecked() || !text.getText().toString().equals("")){
ChecklistAnswer answer = new ChecklistAnswer(questions.get(id).id, 2, cb.isChecked(), text.getText().toString());
answers.add(answer);
}
}
JSONArray json = new JSONArray(answers);
String jsonString = json.toString();
5 answers
Le problème n'est pas le JSONArray.toString (), comme @ Selvin l'a mentionné.
De la source JSONArray:
/**
* Encodes this array as a compact JSON string, such as:
* <pre>[94043,90210]</pre>
*/
@Override public String toString() {
try {
JSONStringer stringer = new JSONStringer();
writeTo(stringer);
return stringer.toString();
} catch (JSONException e) {
return null;
}
}
/**
* Encodes this array as a human readable JSON string for debugging, such
* as:
* <pre>
* [
* 94043,
* 90210
* ]</pre>
*
* @param indentSpaces the number of spaces to indent for each level of
* nesting.
*/
public String toString(int indentSpaces) throws JSONException {
JSONStringer stringer = new JSONStringer(indentSpaces);
writeTo(stringer);
return stringer.toString();
}
Le problème est que vous devez d'abord convertir votre ChecklistAnswer en objet JSON pour que votre JSONArray fonctionne correctement.
Encore de Javadoc:
/**
* A dense indexed sequence of values. Values may be any mix of
* {@link JSONObject JSONObjects}, other {@link JSONArray JSONArrays}, Strings,
* Booleans, Integers, Longs, Doubles, {@code null} or {@link JSONObject#NULL}.
* Values may not be {@link Double#isNaN() NaNs}, {@link Double#isInfinite()
* infinities}, or of any type not listed here.
...
Dans ma classe ChecklistAnwer, j'ai ajouté:
public JSONObject toJsonObject(){
JSONObject json = new JSONObject();
try {
json.put("questionId", questionId);
json.put("checklistId", checklistId);
json.put("answer", answer);
json.put("remark", remark);
} catch (JSONException e) {
e.printStackTrace();
}
return json;
}
Et dans mon autre classe:
JSONArray answers = new JSONArray();
ChecklistAnswer answer = new ChecklistAnswer(questions.get(id).id, 2, cb.isChecked(), text.getText().toString());
answers.put(answer.toJsonObject());
Si j'ai rempli le tableau:
String js = answers.toString(1);
Et qui renvoie:
[
{
"answer": true,
"questionId": 1,
"remark": "",
"checklistId": 2
},
{
"answer": false,
"questionId": 4,
"remark": "teesxfgtfghyfj",
"checklistId": 2
},
{
"answer": true,
"questionId": 4,
"remark": "",
"checklistId": 2
},
{
"answer": true,
"questionId": 4,
"remark": "",
"checklistId": 2
},
{
"answer": true,
"questionId": 4,
"remark": "",
"checklistId": 2
},
{
"answer": true,
"questionId": 4,
"remark": "",
"checklistId": 2
},
{
"answer": true,
"questionId": 4,
"remark": "",
"checklistId": 2
}
]
Merci à @ Selvin
Vous n'avez pas besoin d'utiliser de bibliothèques supplémentaires. La classe JSONArray
a une méthode .toString(int)
supplémentaire qui fait la jolie impression pour vous. Le paramètre int
est le facteur d'indentation.
JSONArray arr = ...
System.out.println(arr.toString(4));
, Il fonctionne également avec un JSONObject
.
Votre plus gros problème est que votre JSONArray
n'est pas construit correctement. Vous êtes censé y ajouter d'autres JSONArray
et JSONObject
, mais vous ajoutez d'autres objets. Ils sont implicitement transformés en String
s. Vous devez les convertir en JSON avant les mettre dans votre tableau.
Vous pouvez utiliser la bibliothèque gson: https://code.google.com/p/google-gson/
Gson gson = new Gson();
String output = gson.toJson(object);
Essayez ceci :
JsonArray jArray = //Your Json Array;
JSONObject jObj = new JSONObject();
jObj.put("test", jArray);
String requiredString = jObj.optString("test");