How to Parse Json in Android

How to parse Json without array name in Android Studio

The response has a JSONArray as root/top element so the type of the Response.Listener should match it. The elements in a JSONArray do not have names, so they are retrieved using the index as you have already done:

public void loadElement() {
String url = "https://periodic-table-api.herokuapp.com/";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++)
{
JSONObject object = response.getJSONObject(i);
element.add(new Element(
object.getString("name"),
object.getString("symbol")
));
}

notifyDataSetChanged();
} catch (JSONException e) {
Log.e("example", "Json error");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("example", "Element list error");
}
});

requestQueue.add(request);
}

How to Parse Json in Kotlin Using Retrofit?

Status, message and data are all part of the response so you need to take care of that. For example this

data class AddUserResponse(
val `data`: UserInfo, //like you defined it
val message: String,
val status: Int,
val time: String
)

This means parameter and response are different so the RestApi needs to be changed to this

abstract fun addUser(@Body userData: UserInfo): Call<AddUserResponse>}

This in turn also change the types in the service like

class RestApiService
{
fun addUser(userData: UserInfo, onResult: (UserInfo?) -> Unit)
{
val retrofit = ServiceBuilder.buildService(RestApi::class.java)
retrofit.addUser(userData).enqueue(
object : Callback<AddUserResponse>
{
override fun onFailure(call: Call<AddUserResponse>, t: Throwable)
{
onResult(null)
}

override fun onResponse( call: Call<AddUserResponse>, response: Response<AddUserResponse>)
{
val addedUser = response.body()
Log.d("responsee",""+addedUser)
onResult(addedUser.data)
}
}
)
}
}

now in getQuotes you will have that it is a UserInfo object

    apiService.addUser(userInfo) {
val returnedUserInfo = it
}

How to Parse a JSON Object In Android

In the end I solved it by using JSONObject.get rather than JSONObject.getString and then cast test to a String.

private void saveData(String result) {
try {
JSONObject json= (JSONObject) new JSONTokener(result).nextValue();
JSONObject json2 = json.getJSONObject("results");
test = (String) json2.get("name");
} catch (JSONException e) {
e.printStackTrace();
}
}

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 data in AS

You missed contents JSONObject.

Copy past below code:

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject contentsObject=response.getJSONObject("contents");
JSONArray jsonArray = contentsObject.getJSONArray("quotes");

for (int i = 0; i < jsonArray.length(); i++) {
JSONObject getQuote = jsonArray.getJSONObject(i);

String quoteOfTheDay = getQuote.getString("quote");

String author = getQuote.getString("author");

quotesView.append(quoteOfTheDay + author + "\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});

How to parse JSON with any key on android?

Yes this is possible.

Put the JSON you receive in a JSONObject. You can loop trough the keys and get the values out of it.

Example:

//Create json object from string
JSONObject newJson = new JSONObject(json);

// Get keys from json
Iterator<String> panelKeys = newJson.keys();

while(panelKeys.hasNext()) {
JSONObject panel = newJson.getJSONObject(panelKeys.next()); // get key from list
String id = panel.getString("id");
String number = panel.getString("number");
}

I hope this is what you were looking for



Related Topics



Leave a reply



Submit