如何把JSON字符串表? [英] How to turn JSON string into table?

查看:93
本文介绍了如何把JSON字符串表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何打开这个JSON:

How to turn this JSON:

{"GetReportResult":
  [
   {"bulan":"4","total":"2448","type":"CHEESE1K"}, 
   {"bulan":"4","total":"572476","type":"ESL"},
   {"bulan":"4","total":"46008","type":"ESL500ML"},
   {"bulan":"5","total":"5703","type":"CHEESE1K"},
   {"bulan":"5","total":"648663","type":"ESL"},
   {"bulan":"5","total":"51958","type":"WHP"},
   {"bulan":"6","total":"6190","type":"CHEESE1K"},
   {"bulan":"6","total":"443335","type":"ESL"},
   {"bulan":"6","total":"30550","type":"ESL500ML"},
  ]
}

进入在Android这样的表:

Into table like this on Android:

---------------------------------------------
|   type   | total1  |   total2   |  total3 |
---------------------------------------------
| CHEESE1K | 6190    |   5703     |  2448   |
| ESL      | 443335  |   648663   |  572476 |
| WHP      | 30550   |   51958    |  46008  |
---------------------------------------------

什么是做的最好的方法?谢谢:)

What is the best way to do that? Thank you :)

是code,我已经取得了迄今为止

推荐答案

简单的方法可能是


  • 解析JSON字符串使用 GSON

  • parse JSON string using GSON

是这样的:

    String json = "{\"GetReportResult\":" + 
"  [" + 
"   {\"bulan\":\"4\",\"total\":\"2448\",\"type\":\"CHEESE1K\"}, " + 
"   {\"bulan\":\"4\",\"total\":\"572476\",\"type\":\"ESL\"}," + 
"   {\"bulan\":\"4\",\"total\":\"46008\",\"type\":\"ESL500ML\"}," + 
"   {\"bulan\":\"5\",\"total\":\"5703\",\"type\":\"CHEESE1K\"}," + 
"   {\"bulan\":\"5\",\"total\":\"648663\",\"type\":\"ESL\"}," + 
"   {\"bulan\":\"5\",\"total\":\"51958\",\"type\":\"WHP\"}," + 
"   {\"bulan\":\"6\",\"total\":\"6190\",\"type\":\"CHEESE1K\"}," + 
"   {\"bulan\":\"6\",\"total\":\"443335\",\"type\":\"ESL\"}," + 
"   {\"bulan\":\"6\",\"total\":\"30550\",\"type\":\"ESL500ML\"}," + 
"  ]" + 
"}"; 
ReportResults reports = new Gson().fromJson(json, ReportResults.class);
List<ReportResult> results = reports.getGetReportResult();
for(ReportResult result:results)System.out.println(result);

该ReportResults类:

The ReportResults class:

package com.yourcomp.test;
import java.util.List;

public class ReportResults {

    private List<ReportResult> GetReportResult;

    /**
     * Gets the getReportResult.
     * 
     * @return <tt> the getReportResult.</tt>
     */
    public List<ReportResult> getGetReportResult() {
        return GetReportResult;
    }

    /**
     * Sets the getReportResult.
     *
     * @param getReportResult <tt> the getReportResult to set.</tt>
     */
    public void setGetReportResult(List<ReportResult> getReportResult) {
        GetReportResult = getReportResult;
    }


}

该ReportResult类:

The ReportResult class:

package com.yourcomp.test;

public class ReportResult {

    private String bulan; 
    private String total;
    private String type;
    /**
     * Gets the bulan.
     * 
     * @return <tt> the bulan.</tt>
     */
    public String getBulan() {
        return bulan;
    }
    /**
     * Sets the bulan.
     *
     * @param bulan <tt> the bulan to set.</tt>
     */
    public void setBulan(String bulan) {
        this.bulan = bulan;
    }
    /**
     * Gets the total.
     * 
     * @return <tt> the total.</tt>
     */
    public String getTotal() {
        return total;
    }
    /**
     * Sets the total.
     *
     * @param total <tt> the total to set.</tt>
     */
    public void setTotal(String total) {
        this.total = total;
    }
    /**
     * Gets the type.
     * 
     * @return <tt> the type.</tt>
     */
    public String getType() {
        return type;
    }
    /**
     * Sets the type.
     *
     * @param type <tt> the type to set.</tt>
     */
    public void setType(String type) {
        this.type = type;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "ReportResult [bulan=" + bulan + ", total=" + total + ", type="
                + type + "]";
    }


}

然后使用解析结果TableLayout显示。

Then use the parsed result to show in TableLayout.

这篇关于如何把JSON字符串表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆