Java 如何将okhttp响应转换为类或列表<;ClassName>;?

Java 如何将okhttp响应转换为类或列表<;ClassName>;?,java,android,gson,okhttp,Java,Android,Gson,Okhttp,我刚接触安卓,正在学习安卓。 我正在使用Okhttp库进行程序中的httpcalls。我为简单的Get请求制作了RESTAPI api/FeaturedCourses(URL) 这是我的回答 [ { "$id": "1", "ExamId": 44, "ImageName": "637064732146027131.jpg", "ExamName": "Some Exam Name", "Description": "Some Long Descrip

我刚接触安卓,正在学习安卓。 我正在使用Okhttp库进行程序中的httpcalls。我为简单的Get请求制作了RESTAPI

api/FeaturedCourses(URL)

这是我的回答

[
  {
    "$id": "1",
    "ExamId": 44,
    "ImageName": "637064732146027131.jpg",
    "ExamName": "Some Exam Name",
    "Description": "Some Long Description Goes Here.",
    "SellPriceUSD": 10,
    "SellPriceINR": 490
  }
]
在okhttp,我到这里都做了

            @Override
            public void onResponse(Call call, Response response) throws IOException {    
                String data = response.body().string();
                nsjdemo.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        txtString.setText(data);
                    }
                });
            }

这是在文本框中显示API响应。但我想将Api响应绑定到列表。所以以后我可以将它绑定到recyclerview或listview。

在搜索了很多之后,我找到了答案,而不需要更改服务器代码

String jsonOutput = response;
Type listType = new TypeToken<List<DemoClass>>(){}.getType();
List<DemoClass> posts = gson.fromJson(jsonOutput, listType);
String jsonOutput=response;
类型listType=newTypeToken(){}.getType();
List posts=gson.fromJson(jsonOutput,listType);

Json和数组之间的主要区别在于{中显示了Json,数组显示在[直接使用Json解析器或使用改型而不是OkHttp(它将帮助您解析Json)@Ashish谢谢你提供的信息。你能给我实现这一点的解决方案吗?所以你想将你的
Json
响应解析为一个响应对象数组,对吗?@Ashish我也尝试过Json响应,但不起作用。