Android - Parsear JSON

 
Vista:
sin imagen de perfil

Parsear JSON

Publicado por Victor (3 intervenciones) el 26/06/2017 20:46:51
Buenas!!!!

Voy un poco perdido en este tema, estoy con este documento JSON y necesito sacar unos datos de el pero no se muy bien como esta montada esta estructura y me pierdo.

Link : http://api.wunderground.com/api/8fec2d58690a48bc/conditions/forecast/lang:CA/q/Spain/Sueca.json

Mi problema lo tengo al sacar los datos osea con este método

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
private void ParsetjaAJSON(String documentJSON){
        Log.d(TAG, "Resposta2: " + documentJSON);
 
        if(documentJSON!=null){
            try {
 
                JSONArray Dies = new JSONArray(documentJSON);
 
 
                for (int i = 0; i<Dies.length();i++){
                    JSONObject jsonObject=Dies.getJSONObject(i);
 
 
                     String Dia = jsonObject.getString("date");
                    String titol = jsonObject.getString("title");
                    String titolnit = jsonObject.getString("title");
                    String precipitacions1 = jsonObject.getString("pop");
                    String precipitacions2 = jsonObject.getString("pop");
                    String descripcio1 = jsonObject.getString("fcttext_metric");
                    String descripcio2 = jsonObject.getString("fcttext_metric");
 
 
                    Dia dia = new Dia();
 
                    dia.setDia(Dia);
                    dia.setTitol(titol);
                    dia.setTitolnit(titolnit);
                    dia.setPrecipitacions1(precipitacions1);
                    dia.setPrecipitacions2(precipitacions2);
                    dia.setDescripcio1(descripcio1);
                    dia.setDescripcio2(descripcio2);
 
                    Log.d(TAG,dia.toString());
 
                    DiesParsetjats.add(dia);
                }
            } catch (JSONException e) {
                Log.e(TAG, "Error parsejant Json: " + e.getMessage());
                Snackbar.make(findViewById(R.id.activity_main),
                        "Error parsejant Json", Snackbar.LENGTH_LONG).show();
            }
        } else {
            Log.e(TAG, "Error intentant rebre el Json.");
            Snackbar.make(findViewById(R.id.activity_main),
                    "Error intentant rebre el Json.", Snackbar.LENGTH_LONG).show();
 
        }
 
    }

La parte en cursiva es donde me marca el error ya que no se como sacar los .getString("Nombre del campo") del JSON que dejo en link.Gracias!!!!.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Parsear JSON

Publicado por Carlos Ismael (38 intervenciones) el 26/06/2017 21:59:52
Hola, en realidad no se donde esta tu error pero te recomiendo que uses la libreria de Google Gson, para el parseo de JSON ya que te facilita muchas cosas, solo necesitas crear una clase POJO de lo que quieres parsear, y tambien para las peticiones que regresan Json ayuda bastante, en internet hay muchos ejemplos de este y para lo que lo ocupas., para crear tus clases puedes utilizar este link el cual te genera las clases automaticamente. http://www.jsonschema2pojo.org/ en base al link que agregaste. Saludos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Parsear JSON

Publicado por Victor (3 intervenciones) el 27/06/2017 10:51:56
Vale he conseguido arreglar algunos fallos que me daba aun asi necesito hacerlo desde JSON, me impiden hacerlo desde GSON.El fallo que tengo por ahora es el siguiente y aun asi no se si con esto me sacara los datos del JSON:

06-27 10:55:07.029 31814-31856/com.example.a2dam.victor E/libEGL: cache file failed CRC check


Aun asi no se como terminar de sacar los datos del JSON y es muy urgente.


Saludos!!!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Parsear JSON

Publicado por Victor (3 intervenciones) el 27/06/2017 19:04:13
Resuelto, pero como último problema que tengo necesito que los textview mati, precip1, desc1 esten alineados uno debajo del otro y lo mismo con los textview nit,precip2 y descrip2.Gracias por la ayuda!!


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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dp">
 
    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        card_view:cardCornerRadius="4dp">
 
        <TextView
            android:id="@+id/Dia"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="20dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingTop="20dp"
            android:text="TextView" />
 
    </android.support.v7.widget.CardView>
 
    <android.support.v7.widget.CardView
        android:id="@+id/card_view2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        card_view:cardCornerRadius="4dp">
 
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
 
            <android.support.v7.widget.CardView
                android:id="@+id/card_view3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="false"
                android:padding="5dp"
                card_view:cardCornerRadius="4dp"
                card_view:cardElevation="9dp">
 
                <TextView
                    android:id="@+id/mati"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingBottom="5dp"
                    android:textAlignment="textStart" />
 
                <TextView
                    android:id="@+id/precip1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@id/mati"
                    android:paddingBottom="5dp"
                    android:text="TextView"
                    android:textAlignment="center" />
 
                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/precip1"
                    android:textAlignment="textEnd">
 
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">
 
                        <TextView
                            android:id="@+id/desc1"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="TextView" />
                    </LinearLayout>
                </ScrollView>
 
            </android.support.v7.widget.CardView>
 
            <android.support.v7.widget.CardView
                android:id="@+id/card_view4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="false"
                android:layout_alignParentStart="false"
                android:layout_alignParentTop="false"
                android:layout_below="@+id/card_view3"
                card_view:cardCornerRadius="4dp">
 
                <TextView
                    android:id="@+id/nit"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:textAlignment="textStart" />
 
                <TextView
                    android:id="@+id/precip2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@id/nit"
                    android:textAlignment="center" />
 
                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/precip2"
                    android:textAlignment="textEnd">
 
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">
 
                        <TextView
                            android:id="@+id/descrip2"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_alignParentTop="false" />
                    </LinearLayout>
                </ScrollView>
            </android.support.v7.widget.CardView>
        </RelativeLayout>
    </android.support.v7.widget.CardView>
 
 
</LinearLayout>
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar