0

I have a JSON output but when I want to read with Retrofit it I am receiving

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 14738 path $.nodes[3].node.image

and but I cannot solve it whatever I do. I don't know where I make mistake.

JSON output

{   "nodes": [
    {
      "node": {
        "body": "text",
        "category": "category",
        "created": "1495941856",
        "id": "2974",
        **"image": [
          {
            "alt": "",
            "src": "http://www.muhabirce.de/sites/default/files/images/news/2017/05/calisma-atasesi_1.jpg",
            "title": "Fazil Arslan Düsseldorf"
          },
          {
            "alt": "",
            "src": "http://www.muhabirce.de/sites/default/files/images/news/2017/05/calisma-atasesi_3.jpg",
            "title": "Türkiye'ye araç götürecek emeklilerin dikkatine! "
          },
          {
            "alt": "",
            "src": "http://www.muhabirce.de/sites/default/files/images/news/2017/05/calisma-atasesi_belge_2.jpg",
            "title": "Türkiye'ye araç götürecek emeklilerin dikkatine! "
          }
        ],**
        "title": "Türkiye'ye araç götürecek emeklilerin dikkatine! ",
        "url": "http://www.muhabirce.de/2017-05-28/turkiyeye-arac-goturecek-emeklilerin-dikkatine"
      }
    },
    {
      "node": {
        "body": "text",
        "category": "category",
        "created": "1496544951",
        "id": "3002",
        **"image": {
          "alt": "",
          "src": "http://www.muhabirce.de/sites/default/files/images/news/2017/06/mustafa_yeneroglu.jpg",
          "title": "Ak Parti İstanbul Milletvekili Mustafa Yeneroğlu "
        },**
        "title": "Köln'de iftar yemeğinde PKK eleştirisi",
        "url": "http://www.muhabirce.de/2017-06-04/kolnde-iftar-yemeginde-pkk-elestirisi"
      }
    },
    ...

My Class are like

public class MainNode {
    public Nodes[] nodes;

    public Nodes[] getNodes() {
        return nodes;
    }

    public void setNodes(Nodes[] nodes) {
        this.nodes = nodes;
    }
}

public class Node {
    public String body;
    public String category;
    public String title;
    public String created;
    public Image[] image;
    public String url;

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCreated() {
        return created;
    }

    public void setCreated(String created) {
        this.created = created;
    }

    public Image[] getImage() {
        return image;
    }

    public void setImage(Image[] image) {
        this.image = image;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}


public class Nodes {
    public Node node;

    public Node getNode() {
        return node;
    }

    public void setNode(Node node) {
        this.node = node;
    }
}


public class Image {
    private String src;

    public String getSrc() {
        return src;
    }

    public void setSrc(String src) {
        this.src = src;
    }
}


interface NewsService {
    String BASE_URL = "url";

    @GET
    Call<MainNode> holderNode(@Url String url);
}
halfer
  • 19,824
  • 17
  • 99
  • 186
nuhkoca
  • 1,777
  • 4
  • 20
  • 44
  • This question seems to have metamorphosed into a brand new question, which invalidates an existing answer. I have therefore rolled it back. Please ask a new question. If your account is prohibited from asking new questions because of the poor reception given to your earlier questions, please read the Meta Stack Overflow documents on how to get unblocked. – halfer Jun 22 '17 at 09:19

1 Answers1

1

Your Node object contains an array of Image objects. In the second node of your JSON the image-node is a JSON-object instead of a JSON-array. GSON throws this exception because it is not possible to map it properly.

If you have created the API you should alter it, so it will always return an json-array, with zero objects, one object, or more objects.

A second solution is to create a custom TypeAdapter for GSON to automatically convert that one object into an Array. It is explained in the accepted answer of this question

Rockney
  • 10,380
  • 2
  • 20
  • 26
  • hi, thanks for the answer. Or can I eliminate objects just get array ones? – nuhkoca Jun 04 '17 at 21:59
  • If you made the api you could change it, so it always returns an array of images, instead of a json-object when there is only one image. Otherwise you should use the answer of this link. – Rockney Jun 04 '17 at 23:19
  • Actually, I didn't make the api and developer is not reachable for the time being and project must have been done and I don't know what should I do. There is only 1 way is to let the type give array in every case. Thanks. – nuhkoca Jun 04 '17 at 23:25
  • In that case you should create a custom TypeAdapter as explained in the link. If you found the answer helpfull, dont forget to mark it as the answer. – Rockney Jun 04 '17 at 23:35
  • Hi, yes I will mark. One thing, I corrected data type and now response body is being null. Any idea? – nuhkoca Jun 05 '17 at 20:18
  • Did you change the datatype of the api, or did you remove the T wildcard of the TypeAdapter – Rockney Jun 06 '17 at 06:13
  • Hi Rockney, in fact I did both of them. Now image is solely acting as an array. Now there is only 1 error: I cannot add Image to higher class via Retrofit. How can I do that? I've changed my question. – nuhkoca Jun 06 '17 at 14:26
  • You are not supposed to rewrite your question into an enterily new question, because all the answers wouldn't make sense anymore. You should create a new question. You probably need to initialize your image list. image = new ArrayList(); – Rockney Jun 09 '17 at 06:54