GSON /杰克逊的Andr​​oid [英] GSON/Jackson in Android

查看:149
本文介绍了GSON /杰克逊的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够使用的JSONObject和JSONArray成功地解析下面的JSON字符串中的Andr​​oid。没有成功实现同样的结果GSON或杰克逊。有人可以帮助我与code片段,包括POJO定义与GSON和杰克逊解析这个?

  {
    响应:{
        状态:200
    },
    项目:
        {
            项目:{
                身:计算
                主题:加减乘除
                附件:假的,
        }
    },
    {
        项目:{
           身:分析
           主题:定量
           附件:真实,
        }
    },

]
分数:10,
 论文:{
        提交:假的,
        头衔:大师
        场:科学,
    }
}
 

解决方案

以下是使用GSON和杰克逊反序列化/序列化JSON(在原来的问题类似无效JSON)的简单的例子来/从一个匹配的Java数据结构

的JSON:

  {
    响应: {
        状态:200
    },
    项目:
        {
            项目: {
                身:计算,
                主题:数学,
                附件:假的
            }
        },
        {
            项目: {
                身:分析,
                主题:定量,
                附件:真实的
            }
        }
    ]
    分数:10,
    论文: {
        提交:假的,
        头衔:大师,
        场:科学
    }
}
 

的匹配Java数据结构:

 类的事
{
  响应响应;
  ItemWrapper []项目;
  INT得分;
  论文论文;
}

级响应
{
  INT状态;
}

类ItemWrapper
{
  项目项目;
}

类项目
{
  字符串的身体;
  串标的;
  布尔附件;
}

类论文
{
  布尔提交;
  字符串称号;
  字符串字段;
}
 

杰克逊举例:

 进口的java.io.File;

。进口组织codehaus.jackson.annotate.JsonAutoDetect.Visibility;
。进口组织codehaus.jackson.map.ObjectMapper;

公共类JacksonFoo
{
  公共静态无效的主要(字串[] args)抛出异常
  {
    ObjectMapper映射器=新ObjectMapper();
    mapper.setVisibilityChecker(
      mapper.getVisibilityChecker()
        .withFieldVisibility(Visibility.ANY));
    事情的事= mapper.readValue(新文件(input.json),Thing.class);
    的System.out.println(mapper.writeValueAsString(事));
  }
}
 

GSON例:

 进口java.io.FileReader;

进口com.google.gson.Gson;

公共类GsonFoo
{
  公共静态无效的主要(字串[] args)抛出异常
  {
    GSON GSON =新GSON();
    事情的事= gson.fromJson(新的FileReader(input.json),Thing.class);
    的System.out.println(gson.toJson(事));
  }
}
 

I was able to successfully parse the below JSON string in Android using JSONObject and JSONArray. Have had no success achieving the same result with GSON or Jackson. Can someone help me with code fragments including POJO definitions to parse this with GSON and Jackson?

{
    "response":{
        "status":200
    },
    "items":[
        {
            "item":{
                "body":"Computing"
                "subject":"Math"
                "attachment":false,
        }
    },
    {
        "item":{
           "body":"Analytics"
           "subject":"Quant"
           "attachment":true,
        }
    },

],
"score":10,
 "thesis":{
        "submitted":false,
        "title":"Masters"
        "field":"Sciences",        
    }
}

解决方案

Following are simple examples of using Gson and Jackson to deserialize/serialize JSON (similar to the invalid JSON in the original question) to/from a matching Java data structure.

The JSON:

{
    "response": {
        "status": 200
    },
    "items": [
        {
            "item": {
                "body": "Computing",
                "subject": "Math",
                "attachment": false
            }
        },
        {
            "item": {
                "body": "Analytics",
                "subject": "Quant",
                "attachment": true
            }
        }
    ],
    "score": 10,
    "thesis": {
        "submitted": false,
        "title": "Masters",
        "field": "Sciences"
    }
}

The Matching Java Data Structure:

class Thing
{
  Response response;
  ItemWrapper[] items;
  int score;
  Thesis thesis;
}

class Response
{
  int status;
}

class ItemWrapper
{
  Item item;
}

class Item
{
  String body;
  String subject;
  boolean attachment;
}

class Thesis
{
  boolean submitted;
  String title;
  String field;
}

Jackson Example:

import java.io.File;

import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.map.ObjectMapper;

public class JacksonFoo
{
  public static void main(String[] args) throws Exception
  {
    ObjectMapper mapper = new ObjectMapper();  
    mapper.setVisibilityChecker(  
      mapper.getVisibilityChecker()  
        .withFieldVisibility(Visibility.ANY));
    Thing thing = mapper.readValue(new File("input.json"), Thing.class);
    System.out.println(mapper.writeValueAsString(thing));
  }
}

Gson Example:

import java.io.FileReader;

import com.google.gson.Gson;

public class GsonFoo
{
  public static void main(String[] args) throws Exception
  {
    Gson gson = new Gson();
    Thing thing = gson.fromJson(new FileReader("input.json"), Thing.class);
    System.out.println(gson.toJson(thing));
  }
}

这篇关于GSON /杰克逊的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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