如何在Android中使用Gson解析JSON?

技术标签: 安卓  杰森  去世

作为 keysArray (a,b,c)是 dynamiccores 有相同的对象名称?请帮忙 !!!

{

 "result":"success",

 "keysArray":[
     "A",
     "B",
     "C",
     "D",
     "E"
     ],

 "cores":{

    "A":[{"key":"value"}],
    "B":[{"key":"value"}],
    "C":[{"key":"value"}],
    "D":[{"key":"value"}],
    "E":[{"key":"value"}]
 }
}

看答案

我以这种方式实现了它。

String responseStr = //JsonResponse here
Type type = new TypeToken<GetMainResponse>() {}.getType();
GetMainResponse getMainResponse = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).create().fromJson(responseStr, type);

    int size = getMainResponse.getKeysArray().size();
    for (int i = 0; i < size; i++) {
        JSONArray jCoreIngredients = new JSONObject(responseStr)
                                            .getJSONArray(getMainResponse.getKeysArray().get(i));
        String innerStr = jCoreIngredients.toString();
        Type type2 = new TypeToken<ArrayList<GetMainResponse.InnerCores>>() {}.getType();
        ArrayList<GetMainResponse.InnerCores> ingredientsInner = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).create().fromJson(innerStr, type2);
    }

//And this is my POJO

public class GetMainResponse{
String result;
ArrayList<String> keysArray;
Cores cores;

public String getResult() { return result; }

public void setResult(String result) { this.result = result; }

public ArrayList<String> getKeysArray() { return keysArray; }

public void setKeysArray(ArrayList<String> keysArray) { this.keysArray = keysArray; }

public Data getCores() { return data; }

public void setCores(Cores cores) { this.cores = cores; }

public class Cores {

// No declaration and not getter setter here.

 public class InnerCores {
    String key;

    public String getKey() { return key; }

    public void setKey(String result) { this.key = key; }

 }
}

有什么建议么 :)


智能推荐

如何在Android Studio中使用JNI

为什么使用jni JNI,java native interface(java本地接口),用于在java中调用本地语言,这里的本地语言一般为C/C++。 一般需要在java中使用C的原因有两个:1.java是基于jvm的,因此无法直接驱动硬件,所以当我们需要直接驱动硬件的时候就需要调用本地语言。 2.C代码的执行效率要远远高于java代码,当我们需要提高效率的时候就要用到jni,比如对于图像的处理...

使用Gson结合泛型类灵活解析JSON数据

=====我自己遇到的问题 因为这个是一个对象,当为空的时候,就会报对象不能被赋值为字符串类型,,, 解决方法:使用泛型 ==================http://blog.csdn.net/yuanguozhengjust/article/details/50477128 一般比较规范的后端JSON返回数据,会遵循一些指定的JSON格式,比如: 错误: [javascript] ...

通过Gson解析Json数据

本文转载自:https://www.cnblogs.com/tianzhijiexian/p/4246497.html 作者:tianzhijiexian 转载请注明该声明。 Json是一种数据格式,便于数据传输、存储、交换; Gson是一种组件库,可以把java对象数据转换成json数据格式。 gson.jar的下载地址:http://search.maven.org/#search%7Cga%...

Gson解析复杂Json数据

背景                                 ...

JSON解析 (Fastjson与Gson)

官网下载2个包 Fastjson:地址https://github.com/alibaba/fastjson Gson: 地址https://github.com/google/gson/ 第二步导入包 1.创建一个lib文件夹将下载好的jar包复制粘贴进去 2.关联 Gson的使用 创建一个book实体类 在main方法中使用Gson 将book转化成一个json对象 执行结果(获取到json)...

猜你喜欢

计算机系统常用编码介绍

常用编码 二进制编码 自然二进制码 有权码 循环二进制码 无权码,卡诺图化简可以用到循环二进制码 BCD码 介绍 把十进制数的各个数码用二进制码的形式表现出来,这便是用二进制代码对十进制数进行编码,简称BCD码。BCD码具有二进制码的形式(4位二进制码),又有十进制数的特点(每四位二进制码是1位十进制码) 常用BCD码 8421码 有权码,8421码也就是自然二进制码 余3码 在8421码的基础上...

好记性不如烂笔头,记我在用友ERP重装IIS服务碰到的一些问题和心得。

先记录文字版的,有时间再上图。 过程是这样的。今天服务器突然断电,导致不能进系统。然后启动时按F8。选择 “进入最后一次正常启动加载项。。。” 可以顺利进系统。 然后点U8登入的时候提示:用友U8   选择数据源提示:读取数据源出现未知错误:〖System.Runtime.Serialization.SerializationException〗 最后查了多方资料...

网友不给力?win10预览版向企业开放

Insider Preview一直以来都是微软windows系统中正式版发布前,预览版发送版本,这个版本只对部分人群开放,类似于一个试点工程,如果用户对系统有意见,可以通过微软官方渠道提供反馈,如果同样的反馈达到一定的量级,微软就会对即将发布的正式版进行改进,吸纳这个建议。但需要注意的是,过去这个功能并没有对windows10企业版、教育版、IOT等版本开放。 实际上企业版windows10毫无疑...

云ids的设计方案

概述 为了搭建云IDS平台,需要选型合适的虚拟化方案,以及对相应部署做一定的优化。基本设计架构为,在以Centos操作系统安装的宿主机中再部署IDS虚拟机(基于Centos操作系统)。 本设计文档所述的设计方案,基于以下环境: Intel x86架构的cpu,支持intel VT和VT-d BIOS中enable VT和VT-d BIOS中建议开启Intel Hyper-Threading Tec...

mysql负载均衡配置

配置负载均衡 选用haproxy原因(自己找文档去吧) haproxy tcp http nginx http ivs tcp 开始: yum install haproxy -y 配置haproxy.cfg文件 vi /etc/haproxy/haproxy.cfg 启动 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg 前往查看负载均衡配置状态 如果出...

问答精选

PHP OOP UNSET object from class in __construct

I have some classes from which I create objects. In the __construct function I check the properties. If they do not match I would like to delete the created object. Thanks! You need to unset the creat...

Reading NSUserDefault bool

I have a problem where I'm trying to register a bool as an NSUserDefault. I then want to be able to read this bool value later on. The issue is that when I read the value its not picking it up as a YE...

deleting values with overlapping dates

I have table in an MS Access database that looks like this: I am trying to delete duplicate symbols and directions that have overlapping start_val and end_val and the highest AW. For example in the ta...

Advice on PHP OOP code

In a PHP project I have three layers (Logical) UI : Place to store UI php pages. ex: ManageUsers.php Code : All Entity Classes Related to system. ex: User.php DataAccess : Data Access Classes for Enti...

Alternate class in a for loop?

I'm cycling through a list using {% for item in list %} and I want to output something like this: with the intention of styling the alternate lines a different colour. Django (1.3) has no mod or div t...

相关问题

相关文章

热门文章

推荐文章

相关标签

推荐问答