Get JSONarray Without Array Name

Get JSONArray without array name?

You don't need to call json.getJSONArray() at all, because the JSON you're working with already is an array. So, don't construct an instance of JSONObject; use a JSONArray. This should suffice:

// ...
JSONArray json = new JSONArray(result);
// ...

for(int i=0;i<json.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = json.getJSONObject(i);

map.put("id", String.valueOf(i));
map.put("name", "Earthquake name:" + e.getString("eqid"));
map.put("magnitude", "Magnitude: " + e.getString("magnitude"));
mylist.add(map);
}

You can't use exactly the same methods as in the tutorial, because the JSON you're dealing with needs to be parsed into a JSONArray at the root, not a JSONObject.

Access JSON Array without array name

response is indeed a jsonObject, you could use it without know its name:

String login_name= response.getString("login_name");

Get JSONArray without array name

Replace the below two lines of code from your class

JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray(result);

From

JSONArray jarray = new JSONArray(data);

That would be sufficient

how to code json array without array name?

Here is a little example:

JSONArray array = new JSONArray();

JSONObject obj1 = new JSONObject();
obj1.put("key", "value");
array.put(obj1);

JSONObject obj2 = new JSONObject();
obj2.put("key2", "value2");
array.put(obj2);

That would look like:

[
{
"key": "value"
},
{
"key2": "value2"
}
]

If you want to get information about your JSONObjects in your JSONArray just iterate over them:

for (int i = 0; i < array.length(); i++) {
JSONObject object = (JSONObject) array.get(i);
object.get("event_id");
object.get("event_title");
}

parse json array with no name

Change this line

StringRequest stringRequest = new StringRequest(Request.Method.GET, "https://api.github.com/users", response -> {

Log.d(TAG, "_ApiGetGithubUsers: " + response);

if (response != null && !response.isEmpty()) {
try {
JSONArray usersArray = new JSONArray(response);

for (int i = 0; i < usersArray.length(); i++) {

JSONObject user = usersArray.getJSONObject(i);
Log.d(TAG, "_ApiGetGithubUsers: "+user.getString("login"));
}

} catch (Exception e) {
Log.d(TAG, "_ApiGetGithubUsers: " + e);
Toast.makeText(context, R.string.someErrorOccurred, Toast.LENGTH_SHORT).show();
}
} else
Toast.makeText(context, R.string.someErrorOccurred, Toast.LENGTH_SHORT).show();

}, error -> {

});

AppController.getInstance().addToRequestQueue(stringRequest);

This is the method i used and it is working and parsing attaching the log image for reference.
Logs

JSON parsing without array name or node in Android

Try this:

 try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i <jsonArray.length() ; i++) {
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
textView.setText(jsonObject.getString("name"));
}
} catch (JSONException e) {
e.printStackTrace();
}

Create a Json Array without the Json Array name

Don't mix JSON elements with your own model. Here's an example using Gson.toJson which produces the expected result:

package test;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;

public class GsonTest {

// Your model class
public static class Test {
private int x;
private int y;
public Test(int x, int y) {
this.x = x;
this.y = y;
}
}

public static void main(String[] args) {
List<Test> list = new ArrayList<>();
list.add(new Test(1, 2));
list.add(new Test(2, 3));
System.out.println(new Gson().toJson(list));

// output: [{"x":1,"y":2},{"x":2,"y":3}]

}

}

How can i write Android Json parsing without array name

It's really simple you just have to get JSONArray object directly like this:

`

@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();

// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);

Log.e(TAG, "Response from url: " + jsonStr);

if (jsonStr != null) {
try {
// Getting JSON Array node
JSONArray contacts = new JSONArray(jsonStr);

// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);

String id = c.getString("nm");
String name = c.getString("cty");
String email = c.getString("hse");
String address = c.getString("yrs");

// tmp hash map for single contact
HashMap<String, String> contact = new HashMap<>();

// adding each child node to HashMap key => value
contact.put("id", id);
contact.put("name", name);
contact.put("email", email);
contact.put("address", address);

// adding contact to contact list
contactList.add(contact);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});

}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});

}

return null;
}

`
Try this.....hope it will work for you.



Related Topics



Leave a reply



Submit