Bonjour ,
Je cherche a parser une liste d'objet JSON en java afin de le récupérer sur mon application mobile.
Malheureusement je n'arrive pas a vérifier si cela s'effectue ou non.
Voila mon JSON (j'ai fait simple pour tester)
Voila ma classe test (J'ai zappé la MAJ desolé :/ )
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 { "etablissement":[ { "id": 111, "nom": "Microsoft" }, { "id": 111, "nom": "Microsoft" } ] }
Voila ma classe me permettant de lire mon JSON
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 package com.example.nice; public class test { private int id; private String nom; public String getNom() { return nom; } public void setNom(String nom) { this.nom = nom; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append("\n id:" + this.id); sb.append("\n nom:" + this.nom); /* if (this.websites != null) { sb.append("\n website: "); for (String website : this.websites) { sb.append(website + ", "); } } if (this.address != null) { sb.append("\n address:" + this.address.toString()); } */ return sb.toString(); } }
Ici lorsque je clique sur le bouton je veux voir s'afficher la taille de ma liste pour voir si j'ai bien réussi a récupérer cela.(Voir directement dans le OnClick)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 package com.example.nice; import android.content.Context; import android.util.Log; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; public class ReadJSONExample { // Read the company.json file and convert it to a java object. public static ArrayList<test> readCompanyJSONFile(Context context) throws IOException,JSONException { // Read content of test.json String jsonText = readText(context, R.raw.test); // On récupère le JSON complet JSONObject jsonRoot = new JSONObject(jsonText); // On récupère le tableau d'objets qui nous concernent //JSONArray array = new JSONArray(jsonRoot.getString("etablissement")); JSONArray array = jsonRoot.getJSONArray("etablissement"); // Liste d'etablissement java ArrayList<test> etablissement = new ArrayList<test>(); //Je parcours la liste des objets présent dans le JSON for (int i = 0; i < array.length(); i++) { JSONObject obj = new JSONObject(array.getString(i)); //Récuperation de l'objet entre JSON et java test test = new test(); test.setId(obj.getInt("id")); //Recuperation et association des variables test.setNom(obj.getString("nom")); //Recuperation et association des variables //Ajout de l'élement dans la liste de l'objet en JAVA etablissement.add(test); } return etablissement; } private static String readText(Context context, int resId) throws IOException { InputStream is = context.getResources().openRawResource(resId); BufferedReader br= new BufferedReader(new InputStreamReader(is)); StringBuilder sb= new StringBuilder(); String s= null; while(( s = br.readLine())!=null) { sb.append(s); sb.append("\n"); } return sb.toString(); } }
Dans notre cas doit s'afficher 2 car il y a deux objets dans le JSON mais je rentre dans l'exception et donc s'afficher : error.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 package com.example.nice; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.view.View; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.widget.EditText; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { private EditText t; @Override //Au lancement de l'application protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); t = (EditText)this.findViewById(R.id.editText); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.addDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); public void onClick(View view) { try { ArrayList<test> test = ReadJSONExample.readCompanyJSONFile(this); t.setText(test.size()); } catch(Exception e) { t.setText("error"); e.printStackTrace(); } } }
Si vous avez une idée pour m'aider a vérifier si j'ai bien réussi a transférer mes objets dans ma liste java.
Merci,
Partager