JSON Parsing in Android

Json Parsing from Url In Android , Not working

As per your response is JSONArray and gson library is better to use while json data parsing so use below class to any type of data like that

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;


public class ApiData {
@SerializedName("data")
@Expose
private JsonArray Data;

public <T> List<T> getData(Class<T> c) {
Type type = new ListParams(c);
return new Gson().fromJson(Data, type);
}

private class ListParams implements ParameterizedType {

private Type type;

private ListParams(Type type) {
this.type = type;
}

@Override
public Type[] getActualTypeArguments() {
return new Type[]{type};
}

@Override
public Type getRawType() {
return ArrayList.class;
}

@Override
public Type getOwnerType() {
return null;
}


@Override
public boolean equals(Object o) {
return super.equals(o);
}

}
}

Create model class like :

public class Model{
String ID;
String Name;
String Contact;
String msg;
}

Now parse your data like:

ApiData apiData = new Gson().fromJson(Content, ApiData.class);
Lis<Model> models = apiData.getData(Model.class);

How to parse a JSON file with volley

Your data is not a JSONObject instead it is a JSONArray so you would use the JSONArrayRequest from Volley and not JSONObjectRequest like you are using. You are probably getting an exception there which you are catching. After you get the JSONArray this line will give you your station for the first station

JSONObject station = jsonArray.getJSONObject(0).getJSONArray("1").getJSONObject(0);

and

station.getString("motto"); 

will give you the motto for the station.

This is because your data structure is very complicated. I would suggest making it better and easier to navigate


This is what your code snippet for request should look like

  JsonArrayRequest jsonArrayRequest = new JsonArrayRequest("https://api.myjson.com/bins/cd6dh",
new Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
JSONObject station1 = response.getJSONObject(0).getJSONArray("1").getJSONObject(0);
String stationName = station1.getString("motto");
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("volley", "error");
}
});
requestQueue.add(jsonArrayRequest);

Turns out that other than the wrong request, you never assigned a listener either. It was null as the second parameter of your request.

How to parse json data with retrofit on Android

I think problem with your URL if you are testing your App with android emulator then try like "http://10.0.2.2:8080/" . but if you are testing with device then you need to pass Your machine IP address like "http://192.143.1.0/". and make sure that your device is connected with your machine on which your database is exits.

How to parse JSON object from a website into an arraylist in android

Use GSON library to parse JSON to java class. It is very simple to use.

Gson gson = new Gson();
Response response = gson.fromJson(jsonLine, Users.class);

Generated model example:

   public class Users {

@SerializedName("Users")
@Expose
public List<User> Users = new ArrayList<User>();
}


public class User {

@SerializedName("name")
@Expose
public String name;

@SerializedName("lon")
@Expose
public double lon;

@SerializedName("lat")
@Expose
public double lat;

}

How to parse JSON with Volley?

Try this:

    String url = "http://api.giphy.com/v1/gifs/search?q=funny+cat&api_key=dc6zaTOxFJmzC";
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jo = jsonArray.getJSONObject(i);
// Do you fancy stuff
// Example: String gifUrl = jo.getString("url");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Anything you want
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);

Android Studio: Parsing JSON Data From 2 Different JSONArrays

You need to make one constructor that can handle all 3 values in a single object for your Reps class.

Reps(String officeName, String party, String officialName) {
//assign value from param to each property.
}

Now change your method from activity as below.

try {
JSONObject jsonObject = new JSONObject(response);
//first loop through offices array. Retrieve officeName value
JSONArray officesArray = jsonObject.getJSONArray("offices"); //One array for offices
JSONArray officialsArray = jsonObject.getJSONArray("officials"); //one array for officials
for (int i = 0; i < officesArray.length(); i++) {
JSONObject jsonoffices = officesArray.getJSONObject(i);
JSONObject jsonofficial = officialsArray.getJSONObject(i);
Reps reps = new Reps(jsonoffices.getString("name"), jsonoffices.getString("party"),
jsonofficial.getString("name"));
repList.add(reps);
adapter = new RepRvAdapter(repList, getApplicationContext());
myrv.setAdapter(adapter);
}
} catch (Exception Ex) {

}


Related Topics



Leave a reply



Submit