JSON 解析为 Java - Android 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6773474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON parsing to Java - Android application
提问by Michal
I need help with parsing json string in Java Android Appl.
我需要在 Java Android Appl 中解析 json 字符串的帮助。
Text of JSON file:
JSON 文件的文本:
{"data":{"columns":["location_id","name","description","latitude","longitude","error","type","type_id","icon_media_id","item_qty","hidden","force_view"],"rows":[[2,"Editor","",43.076014654537,-89.399642451567,25,"Npc",1,0,1,"0","0"],[3,"Dow Recruiter","",43.07550842555,-89.399381822662,25,"Npc",2,0,1,"0","0"] [4,"Protestor","",43.074933,-89.400438,25,"Npc",3,0,1,"0","0"],[5,"State Legislator","",43.074868061524,-89.402136196317,25,"Npc",4,0,1,"0","0"],[6,"Marchers Bascom","",43.075296413877,-89.403374183615,25,"Node",22,0,1,"0","0"] [7,"Mary","",43.074997865584,-89.404967573966,25,"Npc",7,0,1,"0","0"]]},"returnCode":0,"returnCodeDescription":null}
How can get values: location_id, name, latitude, longitude. Thanks, Michal.
如何获取值:location_id、名称、纬度、经度。谢谢,迈克尔。
回答by Maciej
Using manual parsing you can implement it like this:
使用手动解析,您可以像这样实现它:
JSONArray pages = new JSONArray(jsonString);
for (int i = 0; i < pages.length(); ++i) {
JSONObject rec = pages.getJSONObject(i);
JSONObject jsonPage =rec.getJSONObject("page");
String address = jsonPage.getString("url");
String name = jsonPage.getString("name");
String status = jsonPage.getString("status");
}
in your case note that your outer elemnt data is type of JSONObject and then you have a JSONArray
在您的情况下,请注意您的外部元素数据是 JSONObject 类型,然后您有一个 JSONArray
mine json file:
我的json文件:
[{"page":{"created_at":"2011-07-04T12:01:00Z","id":1,"name":"Unknown Page","ping_at":"2011-07-04T12:06:00Z","status":"up","updated_at":"2011-07-04T12:01:00Z","url":"http://www.iana.org/domains/example/","user_id":2}},{"page":{"created_at":"2011-07-04T12:01:03Z","id":3,"name":"Down Page","ping_at":"2011-07-04T12:06:03Z","status":"up","updated_at":"2011-07-04T12:01:03Z","url":"http://www.iana.org/domains/example/","user_id":2}}]
note that mine starts from [, which means an array, but yours from { and then you have [ array inside. If you run it with a debugger, you can see exactly what′s inside your json objects.
请注意,我的从 [ 开始,这意味着一个数组,但你的从 { 开始,然后你有 [ 里面的数组。如果您使用调试器运行它,您可以准确地看到 json 对象中的内容。
There are also better approaches like:
还有更好的方法,例如:
- Hymanson
- Hymanson-JR(light-weight Hymanson)
- GSON
- Hyman逊
- Hymanson-JR(轻量级Hyman逊)
- GSON
All of them can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object.
所有这些都可用于将 Java 对象转换为它们的 JSON 表示。它还可用于将 JSON 字符串转换为等效的 Java 对象。
回答by Paresh Mayani
First of all, you need to know about Json parsing in android, so for that first read this: JSONObject, in that class, you will see the below methods:
首先,您需要了解android中的Json解析,因此首先阅读:JSONObject,在该类中,您将看到以下方法:
- getJSONArray(String name)
- getJSONObject(String name)
- getString(String name)
- getJSONArray(字符串名称)
- getJSONObject(字符串名称)
- getString(字符串名称)
and many more methods to be used while implementing JSON parsing in android.
以及在 android 中实现 JSON 解析时要使用的更多方法。
Update:
更新:
And if you are still confused then click on below link to have many examples available on web: Android JSON Parsing
如果您仍然感到困惑,请单击以下链接以在网络上提供许多示例:Android JSON Parsing
回答by gotch4
If you mean to navigate easily the Json Tree, you can use JSON Path, that is query system, similar to XPath to XML, that you can use to pick some elements in a json tree using text expressions.
如果您想轻松导航 Json 树,您可以使用 JSON Path,即查询系统,类似于 XPath 到 XML,您可以使用它来使用文本表达式在 json 树中选择一些元素。
http://code.google.com/p/json-path/That's a good implementation
http://code.google.com/p/json-path/这是一个很好的实现
If you just mean that you want to parse that JSon you can use Gson from google (that is compatible with Android I guess).
如果你只是想解析那个 JSon,你可以使用谷歌的 Gson(我猜它与 Android 兼容)。
回答by BelmonduS
You need to use the GSON lib http://code.google.com/p/google-gson/
您需要使用 GSON 库 http://code.google.com/p/google-gson/
Object Examples
对象示例
class BagOfPrimitives {
private int value1 = 1;
private String value2 = "abc";
private transient int value3 = 3;
BagOfPrimitives() {
// no-args constructor
}
}
(Serialization)
(序列化)
BagOfPrimitives obj = new BagOfPrimitives();
Gson gson = new Gson();
String json = gson.toJson(obj);
==> json is {"value1":1,"value2":"abc"}
Note that you can not serialize objects with circular references since that will result in infinite recursion.
请注意,您不能使用循环引用序列化对象,因为这会导致无限递归。
(Deserialization)
(反序列化)
BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);
==> obj2 is just like obj