首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从RestAPI的json编码数据创建表?

如何从RestAPI的json编码数据创建表?
EN

Stack Overflow用户
提问于 2019-05-10 15:48:12
回答 3查看 98关注 0票数 0

我正在尝试用wordpress RestAPI中的json数据在MainActivity上创建一个简单的表。我已经能够得到“标题”和“身份”。但我在构建表格时遇到了问题。

json数据看起来像loke:

代码语言:javascript
复制
{
"id": 807,
"date": "2018-08-18T19:06:52",
"date_gmt": "2018-08-18T13:36:52",
"guid": {
    "rendered": "http://wordpresssite.com/?page_id=807"
},
"modified": "2019-04-27T14:32:50",
"modified_gmt": "2019-04-27T09:02:50",
"slug": "sample-post",
"status": "publish",
"type": "page",
"link": "http://wordpresssite.com/sample-page/",
"title": {
    "rendered": "Test results"
},
"content": {
    "rendered": "<div class=\"entry\">\n<p>Check out <strong>Current data</strong> &amp; <strong>Previous Data</strong> here. Among the others, <strong>data</strong> usually is declared first. This page is entirely dedicated to update you with <strong>test data</strong>.</p>\n</script></p>\n<p>&nbsp;</p>\n<h2 style=\"text-align: center;\">data</h2>\n\n<table id=\"tablepress-5\" class=\"tablepress tablepress-id-5\">\n<thead>\n<tr class=\"row-1 odd\">\n\t<th class=\"column-1\">Date</th><th class=\"column-2\">First Round</th><th class=\"column-3\">Second Round</th>\n</tr>\n</thead>\n<tbody class=\"row-hover\">\n<tr class=\"row-2 even\">\n\t<td class=\"column-1\">09/05/19</td><td class=\"column-2\">98</td><td class=\"column-3\">21</td>\n</tr>\n<tr class=\"row-3 odd\">\n\t<td class=\"column-1\">08/05/19</td><td class=\"column-2\">32</td><td class=\"column-3\">91</td>\n</tr>\n<tr class=\"row-4 even\">\n\t<td class=\"column-1\">07/05/19</td><td class=\"column-2\">74</td><td class=\"column-3\">87</td>\n</tr>\n<tr class=\"row-5 odd\">\n\t<td class=\"column-1\">05/05/19</td><td class=\"column-2\">20</td><td class=\"column-3\">27</td>\n</tr>\n<tr class=\"row-6 even\">\n\t<td

这就是我如何为title做的:

代码语言:javascript
复制
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String s) {
            gson = new Gson();
            list = (List) gson.fromJson(s, List.class);
            postTitle = new String[list.size()];

            for(int i=0;i<list.size();++i){
                mapPost = (Map<String,Object>)list.get(i);
                mapTitle = (Map<String, Object>) mapPost.get("title");
                postTitle[i] = (String) mapTitle.get("rendered");
            }

            postList.setAdapter(new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,postTitle));

        }
    },
EN

回答 3

Stack Overflow用户

发布于 2019-05-10 16:37:30

你可以按照下面的方法来设计你的表格视图。您可以基于json数据动态创建视图。

代码语言:javascript
复制
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"
android:layout_height="match_parent"
    android:stretchColumns="0,1,2"
    android:gravity="center">

    <TableRow
        android:background="#FFFFFF"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_margin="1dp"
        android:layout_weight="1"
        >
        <TableRow
            android:background="#000000"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_margin="1dp"
            android:layout_weight="1"
            >



        </TableRow>
    </TableRow>
<TableRow
    android:background="#000000"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_margin="1dp"
    android:layout_weight="1"

    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text=" Date "
        android:layout_margin="1dp"
        android:layout_column="0"
        android:background="#FFFFFF"
        android:textStyle="bold"
        android:gravity="center"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Miles "
        android:layout_margin="1dp"
        android:layout_column="1"
        android:background="#FFFFFF"
        android:gravity="center"
        android:textStyle="bold"
      />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Calories"
        android:layout_margin="1dp"
        android:background="#FFFFFF"
        android:gravity="center"
        android:textStyle="bold"
        android:layout_column="2"
       />
</TableRow>

    <TableRow
        android:background="#000000"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_margin="1dp"
        android:layout_weight="1"
        >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text=" Text"
            android:layout_margin="1dp"
            android:layout_column="0"
            android:background="#FFFFFF"
            android:gravity="center"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text=" Text"
            android:layout_margin="1dp"
            android:layout_column="1"
            android:background="#FFFFFF"
            android:gravity="center"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text=" Text"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:layout_column="2" />
    </TableRow>


</TableLayout>
票数 1
EN

Stack Overflow用户

发布于 2019-05-10 18:39:17

我认为你可以使用JSOUP库来解析你的html内容。

票数 1
EN

Stack Overflow用户

发布于 2019-05-10 17:33:48

我期待以下内容作为您的输入:

代码语言:javascript
复制
{
    "id": 807,
    "date": "2018-08-18T19:06:52",
    "date_gmt": "2018-08-18T13:36:52",
    "guid": {
        "rendered": "http://wordpresssite.com/?page_id=807"
    },
    "modified": "2019-04-27T14:32:50",
    "modified_gmt": "2019-04-27T09:02:50",
    "slug": "sample-post",
    "status": "publish",
    "type": "page",
    "link": "http://wordpresssite.com/sample-page/",
    "title": {
        "rendered": "Test results"
    },
    "content": {
        "rendered": "your content...."
    }
}

现在,您可以通过以下方式解析此数据:

1)在gradel文件中添加依赖项

代码语言:javascript
复制
dependencies {
  implementation 'com.google.code.gson:gson:2.8.5'
}

2)创建pojo类:

代码语言:javascript
复制
-----------------------------------com.example.Content.java-----------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Content {

@SerializedName("rendered")
@Expose
private String rendered;

public String getRendered() {
return rendered;
}

public void setRendered(String rendered) {
this.rendered = rendered;
}

}
-----------------------------------com.example.Example.java-------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("date")
@Expose
private String date;
@SerializedName("date_gmt")
@Expose
private String dateGmt;
@SerializedName("guid")
@Expose
private Guid guid;
@SerializedName("modified")
@Expose
private String modified;
@SerializedName("modified_gmt")
@Expose
private String modifiedGmt;
@SerializedName("slug")
@Expose
private String slug;
@SerializedName("status")
@Expose
private String status;
@SerializedName("type")
@Expose
private String type;
@SerializedName("link")
@Expose
private String link;
@SerializedName("title")
@Expose
private Title title;
@SerializedName("content")
@Expose
private Content content;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getDateGmt() {
return dateGmt;
}

public void setDateGmt(String dateGmt) {
this.dateGmt = dateGmt;
}

public Guid getGuid() {
return guid;
}

public void setGuid(Guid guid) {
this.guid = guid;
}

public String getModified() {
return modified;
}

public void setModified(String modified) {
this.modified = modified;
}

public String getModifiedGmt() {
return modifiedGmt;
}

public void setModifiedGmt(String modifiedGmt) {
this.modifiedGmt = modifiedGmt;
}

public String getSlug() {
return slug;
}

public void setSlug(String slug) {
this.slug = slug;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getLink() {
return link;
}

public void setLink(String link) {
this.link = link;
}

public Title getTitle() {
return title;
}

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

public Content getContent() {
return content;
}

public void setContent(Content content) {
this.content = content;
}

}
--------------com.example.Guid.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Guid {

@SerializedName("rendered")
@Expose
private String rendered;

public String getRendered() {
return rendered;
}

public void setRendered(String rendered) {
this.rendered = rendered;
}

}
-----------------------------------com.example.Title.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Title {

@SerializedName("rendered")
@Expose
private String rendered;

public String getRendered() {
return rendered;
}

public void setRendered(String rendered) {
this.rendered = rendered;
}

}

3)使用Gson库解析数据,结果如下:

代码语言:javascript
复制
Gson gson = new Gson();

String stringToParse =""; //string you want to parse.

Example response = gson.fromJson(stringToParse , Example.class);

Content content = response.getContent();

希望这能对你有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56073025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档