Je veux créer une chaîne de requête json. Comment puis je faire ça? C'est mon tableau suivant:
[1,2,3,4,5];
Maintenant, je veux créer ce type de requête json:
{
"contact_approve":[
{
"contact_id":1
},
{
"contact_id":2
},
{
"contact_id":3
},
{
"contact_id":4
},
{
"contact_id":5
},
]
}
Quelqu'un a-t-il l'idée de créer ce type de chaîne json?
5 réponses
Voici le code:
try {
JSONObject jsonObject = new JSONObject();
JSONArray array = new JSONArray();
int[] dataArray = {1,2,3,4,5};
for( int i=0;i<dataArray.length;i++) {
JSONObject internalObject = new JSONObject();
internalObject.put("contact_id",dataArray[i]);
array.put(internalObject);
}
jsonObject.put("contact_approve", array);
System.out.print(jsonObject);
Log.v("JsonObject",jsonObject.toString());
} catch (JSONException e) {
e.printStackTrace();
}
Merci
JSONObject qui commencent par {} JSONArray qui commencent par [] Dans votre cas, vous avez un JSONObject qui a un JSONArray.
Laisse moi te donner un exemple.
public void createJson() throws JSONException {
// Create main JSON object
JSONObject jsonObject = new JSONObject();
//Create an JSON ARRAY which will have a key "contact_approve" later on
JSONArray jsonArray = new JSONArray();
Integer contacts[] = {1,2,3,4,5};
for (int i = 0; i < contacts.length ; i++) {
JSONObject contactID = new JSONObject();
jsonArray.put(contactID.put("contact_id", i));
}
jsonObject.put("contact_approve", jsonArray);
String json = jsonObject.toString();
Log.d("JSON", json);
}
Incase si vous avez besoin d'analyser la réponse JSON, jetez un œil aux bibliothèques Moshi et GSON qui améliorent votre journée. Ces bibliothèques peuvent analyser les classes JAVA en JSON. J'utilise [https://codebeautify.org/jsonviewer] pour afficher ou réduire le json.
Bonne chance.
Parcourez le tableau et affichez la chaîne appropriée pour chaque élément.
public static String ConvertToContactJson(int [] array)
{
String finalstr = "{\"contact_approve\":[";
for (int i=0; i< array.length; i++)
{
finalstr += "{\"contact_id\":"+array[i]+"}";
if (i < array.length-1) finalstr += ",";
}
finalstr += "]}";
return finalstr;
}
Essaye ça
try{
JSONArray jarray=new JSonArray();
for(int i=0;i<array.size();i++){
JSONObject j=new JSONObject();
j.put("contact_id",array[i]);
jarray.put(j);
}
}catch(Exception e){
e.printStackStrace();
}
Eh bien, c'est simple, vous pouvez utiliser la bibliothèque Gson pour convertir des objets en json comme ci-dessous;
Gson gson = new Gson();
String json = gson.toJson({your object you want to convert to json});
Une autre chose à mentionner est que si vous envoyez une demande au serveur, vous n'avez même pas besoin de faire la sérialisation json vous-même, utilisez la modernisation et il le fera pour vous.
Questions connexes
De nouvelles questions
android
Android est le système d'exploitation mobile de Google, utilisé pour la programmation ou le développement d'appareils numériques (Smartphones, tablettes, automobiles, téléviseurs, Wear, Glass, IoT). Pour les sujets liés à Android, utilisez des balises spécifiques à Android telles que l'intention d'Android, l'activité d'Android, l'adaptateur Android, etc. Pour les questions autres que le développement ou la programmation, mais liées au cadre Android, utilisez ce lien: https: // android.stackexchange.com.