如何通过Retrofit 2使用此JSON结构? [英] How to consume this JSON structure via Retrofit 2?
问题描述
你好,我有一个json结构,我需要从url中获取数据.我怎样才能做到这一点?什么是类和函数.请帮我.谢谢.
Hello I have a json structure and I need to get the datas from url. How can I do that? What are classes and functions. Please help me. Thanks.
这是我的json.
{
nodes: [
{
node: {
title: "...",
field_news_image: [
{title="..."},
{title="..."},
{title="..."},
{title="..."},
{title="..."},
{title="..."},
{title="..."}
],
body: "...",
field_category: "...",
created: "..."
}
},
{
node: {
title: "...",
field_news_image: [
{title="..."},
{title="..."},
{title="..."},
{title="..."},
{title="..."}
],
body: "...",
field_category: "...",
created: "..."
}
}
]
}
推荐答案
http://www.jsonschema2pojo. org/
http://www.jsonschema2pojo.org/
在此处放置JSON响应,然后从此网站下载Java代码.然后,将此Java代码放入您的项目中并使用它.该网站将使用所有必填字段制作所有必填类,非常适合您的JSON响应.我已经使用了很多次,这肯定会起作用.
Put your JSON response here and download java code from this website . Then put this java code into your project and use it . This website will make all required classes with all required fields which will be perfect fit for your response of JSON. I have used it many time this will surely work.
http://square.github.io/retrofit/ 在这里,您可以获得有关如何对POST或GET数据进行改造的详细信息.
http://square.github.io/retrofit/ here you can get detailed information about how to use retrofit to POST or GET data .
Retrofit将您的HTTP API转换为Java接口.
Retrofit turns your HTTP API into a Java interface.
public interface YourService {
@GET("users/{user}/repos")
Call<List<Repository>> listRepos(@Path("user") String user);
}
Retrofit类生成接口的实现.
The Retrofit class generates an implementation of the interface.
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.yourAPI.com/")
.build();
YourService service = retrofit.create(YourService .class);
Call<List<Repository>> repos = service.listRepos("myrepository");
使用注释描述 HTTP 请求:
- URL参数替换和查询参数支持对象 转换为请求正文(例如JSON,协议缓冲区)多部分 请求正文和文件上传
- URL parameter replacement and query parameter support Object conversion to request body (e.g., JSON, protocol buffers) Multipart request body and file upload
这篇关于如何通过Retrofit 2使用此JSON结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!