0

I tried to parse this JSON address but failed. The code I tried is failing, no data is reflected. Can you help me?

My Code;

private void parseJSON() {
    final String url = "json website";
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray jsonArray = response.getJSONArray(null);
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject hit = jsonArray.getJSONObject(i);
                            String imageref = hit.getString(null);

                            mExampleList.add(new ExampleItemRef(imageref));
                        }

This JSON;

[
"https://www.asdasd.com/images/r/a.png",
"https://www.asdasd.com/images/r/b.png",
"https://www.asdasd.com/images/r/c.png",
"https://www.asdasd.com/images/r/d.png",
"https://www.asdasd.com/images/r/e.png"
]
  • From the code you provided, it looks like you're iterating through a new JSON array that you created (`new JSONArray()` creates an empty JSON array object), which means it will contain no elements. You should be obtaining the array containing the strings. Without knowing the source of your JSON (e.g. service response, programmatically created object, JSON string?), it is hard to provide a complete answer. If the source is simply a JSON string, you can check out this answer: https://stackoverflow.com/questions/18977144/how-to-parse-json-array-not-json-object-in-android – VIN Nov 18 '18 at 03:08
  • empty json do not return my code as follows: JSONArray jsonArray = response.getJSONArray(null); – user10669131 Nov 18 '18 at 03:41
  • I edited the message, can you see? – user10669131 Nov 18 '18 at 03:45

2 Answers2

0

Just remove all unnecessary part from your code and change it like below. You need to put your json string when creating array

private void parseJSON() {
    final String url = "json website";
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray jsonArray = response.getJSONArray(jsonString);
                          for (int i = 0; i < jsonArray.length(); i++) {
                                    mExampleList.add(new ExampleItemRef(jsonArray.getString(i)));
                                }
                        }
PushpikaWan
  • 2,437
  • 3
  • 14
  • 23
0

You could do like this:

ArrayList < ExampleItemRef > mExampleList = new ArrayList <ExampleItemRef > ();
String jsonStr =
    "[ \"https://www.asdasd.com/images/r/a.png\", \"https://www.asdasd.com/images/r/b.png\", \"https://www.asdasd.com/images/r/c.png\", \"https://www.asdasd.com/images/r/d.png\", \"https://www.asdasd.com/images/r/e.png\" ]";

try {
    JSONArray jsonArray = new JSONArray(jsonStr);
    for (int i = 0; i < jsonArray.length(); i++) {
        String imageref = jsonArray.getString(i);
        mExampleList.add(new ExampleItemRef(imageref));
    }
} catch (JSONException e) {
    e.printStackTrace();
}

codes are self explanatory, so no comments,hope helpful

Edited: try this:

private void parseJSON() {
    final String url = "json website";
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray jsonArray = new JSONArray(response.toString());
                        for (int i = 0; i < jsonArray.length(); i++) {
                           String imageref = jsonArray.getString(i);
                           mExampleList.add(new ExampleItemRef(imageref));
                        }
navylover
  • 12,383
  • 5
  • 28
  • 41
  • hey, thenks but my json not string. in this way; final String url = "JSON WEBSITE"; JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener() { @Override public void onResponse(JSONObject response) { try { JSONArray jsonArray = response.getJSONArray(null); – user10669131 Nov 18 '18 at 03:42