如何从 JSON 字符串中获取数据?

发布于 2025-01-07 17:24:31 字数 562 浏览 0 评论 0原文

我有一个像这样的 JSON 字符串:

{
    "response":[2,
        {
            "id":"187",
            "name":"John",
            "surname":"Corner"
        },
        {
            "id":"254",
            "name":"Bob",
            "surname":"Marley"
        }
    ]
}

How can I parse it using GSON lib in Java? 我尝试过:

static class SearchRequest {
    private Names[] response;

    static class Names {
        private int id;
        private String name;
        private String surname;
    }
}

但不起作用:(

I have a JSON string like this:

{
    "response":[2,
        {
            "id":"187",
            "name":"John",
            "surname":"Corner"
        },
        {
            "id":"254",
            "name":"Bob",
            "surname":"Marley"
        }
    ]
}

How can I parse it using GSON lib in Java?
I tried:

static class SearchRequest {
    private Names[] response;

    static class Names {
        private int id;
        private String name;
        private String surname;
    }
}

but it doesn't works :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独夜无伴 2025-01-14 17:24:31

response 数组不仅包含Names,还包含一个整数值:response[0]=2
所以你必须使用类似

private Object[] response;

ADD: 的东西
我认为,您无需更改 JSON,因为您的 response 是私有的,因此您应该使用 getter:

static class SearchRequest {
    private Object[] response;

    static class Names {
        private int id;
        private String name;
        private String surname;
    }

    public List<Names> getNames() {
        List list = new ArrayList();
        list.addAll(Arrays.asList(response));
        return (List<Names>) list.subList(1, response.length);
    }

    public int getAmount() {
        return (Integer) response[0];
    }

    public void setNames(List<Names> names) {
        response = new Object[names.size() + 1];
        response[0] = names.size();
        for (int i = 0, namesSize = names.size(); i < namesSize; i++) {
            response[i + 1] = names.get(i);
        }
    }
}

response array contains not only Names, it also contains an integer value: response[0]=2.
So you must use something like

private Object[] response;

ADD:
I think, you have no need to change your JSON, because your response is private, so you should use getters:

static class SearchRequest {
    private Object[] response;

    static class Names {
        private int id;
        private String name;
        private String surname;
    }

    public List<Names> getNames() {
        List list = new ArrayList();
        list.addAll(Arrays.asList(response));
        return (List<Names>) list.subList(1, response.length);
    }

    public int getAmount() {
        return (Integer) response[0];
    }

    public void setNames(List<Names> names) {
        response = new Object[names.size() + 1];
        response[0] = names.size();
        for (int i = 0, namesSize = names.size(); i < namesSize; i++) {
            response[i + 1] = names.get(i);
        }
    }
}
栀梦 2025-01-14 17:24:31

我准备了一个示例代码来测试您的案例,使用 gson-2.1.jar 作为库并正确运行它。我通过从 response 数组的开头删除 "2," 来修改您的 JSON 字符串,因为它导致了异常。查看并测试此代码:

import com.google.gson.Gson;

public class Test
{
    static class SearchRequest
    {
        private Names [] response;

        static class Names
        {
            private int id;
            private String name;
            private String surname;
        }
    }

    public static void main ( String [] args )
    {
        Gson gson = new Gson();
        String str = "{ \"response\":" 
                   + "[{\"id\":\"187\",\"name\":\"John\",\"surname\":\"Corner\"},"
                   + "{\"id\":\"254\",\"name\":\"Bob\",\"surname\":\"Marley\"}]}";

        SearchRequest request = new SearchRequest();
        request = gson.fromJson( str, SearchRequest.class );
        System.out.println( "name of 1st: " + request.response [ 0 ].name );
        System.out.println( "surname of 2nd: " + request.response [ 1 ].surname );
    }
}

输出:

name of 1st: John
surname of 2nd: Marley

I prepared a sample code for testing your case, used gson-2.1.jar as library and run it correctly. I modified your JSON string by removing "2," from the beginning of response array since it caused exceptions. Take a look at and test this code:

import com.google.gson.Gson;

public class Test
{
    static class SearchRequest
    {
        private Names [] response;

        static class Names
        {
            private int id;
            private String name;
            private String surname;
        }
    }

    public static void main ( String [] args )
    {
        Gson gson = new Gson();
        String str = "{ \"response\":" 
                   + "[{\"id\":\"187\",\"name\":\"John\",\"surname\":\"Corner\"},"
                   + "{\"id\":\"254\",\"name\":\"Bob\",\"surname\":\"Marley\"}]}";

        SearchRequest request = new SearchRequest();
        request = gson.fromJson( str, SearchRequest.class );
        System.out.println( "name of 1st: " + request.response [ 0 ].name );
        System.out.println( "surname of 2nd: " + request.response [ 1 ].surname );
    }
}

Output:

name of 1st: John
surname of 2nd: Marley
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文