简体   繁体   English

JSON 解析为 Java - Android 应用

[英]JSON parsing to Java - Android application

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.如何获取值:location_id、名称、纬度、经度。 Thanks, Michal.谢谢,迈克尔。

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:还有更好的方法,例如:

  1. Jackson Jackson
  2. Jackson-JR (light-weight Jackson) Jackson-JR (轻量级杰克逊)
  3. GSON GSON

All of them can be used to convert Java Objects into their JSON representation.它们都可用于将 Java 对象转换为它们的 JSON 表示。 It can also be used to convert a JSON string to an equivalent Java object.它还可用于将 JSON 字符串转换为等效的 Java object。

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 ,在 class 中,您将看到以下方法:

  1. getJSONArray(String name) getJSONArray(字符串名称)
  2. getJSONObject(String name) getJSONObject(字符串名称)
  3. getString(String name) 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如果您仍然感到困惑,请单击下面的链接以获取 web 上的许多示例: Android JSON

You need to use the GSON lib http://code.google.com/p/google-gson/您需要使用 GSON 库http://code.google.com/p/google-gson/

Object Examples Object 示例

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

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. 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.

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 兼容)。

This contains a complete example for your case. 包含您案例的完整示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM