简体   繁体   中英

How to get arrays from JSON using retrofit in Android?

This is my JSON

   [{
        "user_name": "name",
        "tags": ["p", "a", "py"],
        "a": 3,
        "b": 12,
        "c": 4
    },
    {
        "user_name": "name2",
        "tags": ["p2", "a2", "py2"],
        "a": 32,
        "b": 122,
        "c": 42
    }]

I have no problems using retrofit to get all the other values, except tags. How do I do it?

If you provide some code of what you are actually using currently that would help us help you better. In any case since you mentioned you are doing these already I am assuming you have created the POJO class for this response with something like

public class YourResponse
{
    private String a;

    private String b;

    private String c;

    private String user_name;

    private String[] tags;

....

}

Now when you write a call to the api, since its a list of this class' objects you should write as

Call<List<YourResponse>> call = ...

and similarly in callbacks etc. like call.enqueue(new Callback<List<YourResponse>>()......

Edit: Just noticed you mentioned your problem was only with getting the values for "tags" and not the response. Your question is already answered by @Mark Keen in his comment. -" tags should be a List<String> , or String[] "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM