如何转换一个JSONObject类对象? [英] How do I convert a JSONObject to class object?

查看:158
本文介绍了如何转换一个JSONObject类对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用GSON库一个JSONObject转换为定位对象为我的Andr​​oid项目。我不知道如何做到这一点。谁能帮我这个。先谢谢了。

I need to convert a JSONObject to a Location object using gson libraries for an android project of mine. I'm not sure on how to do this. Can anyone help me with this. Thanks in advance.

我有一个code像

JSONArray input = new JSONArray(extras.getString("loc_json"));

我想从JSONArray带到一个位置类对象的JSONObject进行转换。我只是想知道是否有直接做它的功能。

I wanted to convert the JSONObject taken from the JSONArray to a Location class object. I just wanted to know whether there is a function that does it directly.

请原谅我,如果我在一个错误的方式陷害的问题。因为我没有直接的作用,我做了这个样子。

Pardon me if I framed the question in a wrong way. Since I haven't got the direct function, I did it in this way.

 loc_temp = (Location) gson.fromJson(input.getJSONObject(i).toString(), Location.class);;

对不起,愚蠢的问题。

Sorry for the stupid question.

推荐答案

下面是GSON教程 - 我想应该有助于加强你的理解。本质上分析是这样做的:

Here's a tutorial for GSON - I think it should help bolster your understanding. Essentially the parsing is done like this:

String mJsonString = "..."
JsonElement mJson =  new JsonParser.parse(mJsonString);

MyDataObject object = gson.fromJson(mJson, MyDataObject.class);

这篇关于如何转换一个JSONObject类对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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