使用gson反序列化对象的特定JSON字段 [英] Using gson to deserialize specific JSON field of an object

查看:98
本文介绍了使用gson反序列化对象的特定JSON字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下JSON字符串:

  {
ms:images,5160.1,
turl:http://ts1.mm.bing.net/th?id=I4693880201938488&pid=1.1,
height:178,
width :300,
imgurl:http://www.attackingsoccer.com/wp-content/uploads/2011/07/World-Cup-2012-Draw.jpg,
抵消:0,
t:2014年世界杯资格赛 - 欧洲平局2012世界杯平局......,
w:719,
h :427,
ff:jpeg,
fs:52,
durl:www.attackingsoccer.com/2011/07/world -cup-2012-qualification-europe ...,
surl:http://www.attackingsoccer.com/2011/07/world-cup-2012-qualification-europe-draw/world- cup-2012-draw /,
mid:D9E91A0BA6F9E4C65C82452E2A5604BAC8744F1B,
k:6,
ns:API.images
}

我需要存储 imgurl 在一个单独的字符串中。



这就是我所持续的l现在,但这只是给了我整个JSON字符串,而不是特定的imgurl字段。



pre $ g $ gson = new Gson() ;
Data data = new Data();
data = gson.fromJson(toExtract,Data.class);
System.out.println(data);

toExtract 是JSON字符串。
这里是我的数据类:

  public class Data 
{
public List< urlString> ; myurls;
}

class urlString
{
String imgurl;
}


解决方案

解析这样一个简单的结构,不需要专门的课程。



解决方案1:

你可以这样做:

  JsonParser parser = new JsonParser(); 
JsonObject obj = parser.parse(toExtract).getAsJsonObject();
String imgurl = obj.get(imgurl)。getAsString();

这使用原始解析到 JsonObject

解决方案2 :

或者,您可以使用

<

 属性data = gson.fromJson(toExtract,Properties.class); 

并读取您的网址

  String imgurl = data.getProperty(imgurl); 


I have the following JSON string:

{
    "ms": "images,5160.1",
    "turl": "http://ts1.mm.bing.net/th?id=I4693880201938488&pid=1.1",
    "height": "178",
    "width": "300",
    "imgurl": "http://www.attackingsoccer.com/wp-content/uploads/2011/07/World-Cup-2012-Draw.jpg",
    "offset": "0",
    "t": "World Cup 2014 Qualification – Europe Draw World Cup 2012 Draw ...",
    "w": "719",
    "h": "427",
    "ff": "jpeg",
    "fs": "52",
    "durl": "www.attackingsoccer.com/2011/07/world-cup-2012-qualification-europe...",
    "surl": "http://www.attackingsoccer.com/2011/07/world-cup-2012-qualification-europe-draw/world-cup-2012-draw/",
    "mid": "D9E91A0BA6F9E4C65C82452E2A5604BAC8744F1B",
    "k": "6",
    "ns": "API.images"
}

I need to store the value of imgurl in a separate string.

This is what I have till now, but this just gives me the whole JSON string instead of the specific imgurl field.

Gson gson = new Gson();
Data data = new Data();
data = gson.fromJson(toExtract, Data.class);
System.out.println(data);

toExtract is the JSON string. Here is my data class:

public class Data 
{
    public List<urlString> myurls;
}

class urlString
{
    String imgurl;
}

解决方案

When parsing such a simple structure, no need to have dedicated classes.

Solution 1 :

To get the imgurURL from your String with gson, you can do this :

JsonParser parser = new JsonParser();
JsonObject obj = parser.parse(toExtract).getAsJsonObject();
String imgurl = obj.get("imgurl").getAsString();

This uses a raw parsing into a JsonObject.

Solution 2 :

Alternatively, you could extract your whole data in a Properties instance using

 Properties data = gson.fromJson(toExtract, Properties.class);

and read your URL with

String imgurl = data.getProperty("imgurl");

这篇关于使用gson反序列化对象的特定JSON字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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