Android中使用GSON解析复杂的JSON对象 [英] Using GSON in Android to parse a complex JSON object

查看:26
本文介绍了Android中使用GSON解析复杂的JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Java 编程比较陌生,需要通过网络解析复杂的 JSON 对象.过去一天我一直在阅读有关 GSON 的文档,但没有多少运气能够完全解析这种类型的结构:

I'm relatively new to Java programming and need to parse a complex JSON object across the wire. I've been reading documentation on GSON the past day and Haven't had much luck being able to fully parse this type of structure:

{
  'Events' : [{
    'name' : 'exp',
    'date' : '10-10-2010',
    'tags' : ["tag 1", "tag2", "tag3"]
    },...more events...],
  'Contacts' : [{
    'name' : 'John Smith',
    'date' : '10-10-2010',
    'tags' : ["tag 1", "tag2", "tag3"]
    },...more contacts...],
}

我已经能够让它与这个问题类似,但不知道如何获得额外的数组级别才能工作.

I've been able to get it to work similarly to this question but can't figure out how to get that additional array level to work.

推荐答案

以我正在寻找的格式使用 GSON 的正确方法是:

The correct way to do it using GSON in the format I'm looking for is:

//somewhere after the web response:
Gson gson = new Gson();

Event[] events = gson.fromJson(webServiceResponse,  Event[].class);


//somewhere nested in the class:
static class Event{
    String name;
    String date;

    public String getName()
    {
        return name;
    }

    public String getDate()
    {
        return date;
    }

    public void setName(String name)
    {
        this.name = name;
    }

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

这篇关于Android中使用GSON解析复杂的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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