在 Java 中解析 JSON 字符串 [英] Parsing JSON string in Java

查看:39
本文介绍了在 Java 中解析 JSON 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Java 中解析 JSON 字符串以单独打印单个值.但是在运行程序时出现以下错误-

I am trying to parse a JSON string in java to have the individual value printed separately. But while making the program run I get the following error-

Exception in thread "main" java.lang.RuntimeException: Stub!
       at org.json.JSONObject.<init>(JSONObject.java:7)
       at ShowActivity.main(ShowActivity.java:29)

我的班级看起来像-

import org.json.JSONException;
import org.json.JSONObject;

public class ShowActivity {
   private final static String  jString = "{" 
   + "    "geodata": [" 
   + "        {" 
   + "                "id": "1"," 
   + "                "name": "Julie Sherman","                  
   + "                "gender" : "female"," 
   + "                "latitude" : "37.33774833333334"," 
   + "                "longitude" : "-121.88670166666667""            
   + "                }" 
   + "        }," 
   + "        {" 
   + "                "id": "2"," 
   + "                "name": "Johnny Depp","          
   + "                "gender" : "male"," 
   + "                "latitude" : "37.336453"," 
   + "                "longitude" : "-121.884985""            
   + "                }" 
   + "        }" 
   + "    ]" 
   + "}"; 
   private static JSONObject jObject = null;

   public static void main(String[] args) throws JSONException {
       jObject = new JSONObject(jString);
       JSONObject geoObject = jObject.getJSONObject("geodata");

       String geoId = geoObject.getString("id");
           System.out.println(geoId);

       String name = geoObject.getString("name");
       System.out.println(name);

       String gender=geoObject.getString("gender");
       System.out.println(gender);

       String lat=geoObject.getString("latitude");
       System.out.println(lat);

       String longit =geoObject.getString("longitude");
       System.out.println(longit);                   
   }
}

让我知道我遗漏了什么,或者我每次运行应用程序时都会收到该错误的原因.任何意见将不胜感激.

Let me know what is it I am missing, or the reason why I do get that error everytime I run the application. Any comments would be appreciated.

推荐答案

见我的评论.作为 android.jar 运行时,您需要包含完整的 org.json 库 只包含要编译的存根.

See my comment. You need to include the full org.json library when running as android.jar only contains stubs to compile against.

此外,您必须删除 JSON 数据中 longitude 后面的两个额外 } 实例.

In addition, you must remove the two instances of extra } in your JSON data following longitude.

   private final static String JSON_DATA =
     "{" 
   + "  "geodata": [" 
   + "    {" 
   + "      "id": "1"," 
   + "      "name": "Julie Sherman","                  
   + "      "gender" : "female"," 
   + "      "latitude" : "37.33774833333334"," 
   + "      "longitude" : "-121.88670166666667""
   + "    }," 
   + "    {" 
   + "      "id": "2"," 
   + "      "name": "Johnny Depp","          
   + "      "gender" : "male"," 
   + "      "latitude" : "37.336453"," 
   + "      "longitude" : "-121.884985""
   + "    }" 
   + "  ]" 
   + "}"; 

除此之外,geodata 实际上不是 JSONObject 而是 JSONArray.

Apart from that, geodata is in fact not a JSONObject but a JSONArray.

以下是完全正常工作并经过测试的更正代码:

Here is the fully working and tested corrected code:

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ShowActivity {


  private final static String JSON_DATA =
     "{" 
   + "  "geodata": [" 
   + "    {" 
   + "      "id": "1"," 
   + "      "name": "Julie Sherman","                  
   + "      "gender" : "female"," 
   + "      "latitude" : "37.33774833333334"," 
   + "      "longitude" : "-121.88670166666667""
   + "    }," 
   + "    {" 
   + "      "id": "2"," 
   + "      "name": "Johnny Depp","          
   + "      "gender" : "male"," 
   + "      "latitude" : "37.336453"," 
   + "      "longitude" : "-121.884985""
   + "    }" 
   + "  ]" 
   + "}"; 

  public static void main(final String[] argv) throws JSONException {
    final JSONObject obj = new JSONObject(JSON_DATA);
    final JSONArray geodata = obj.getJSONArray("geodata");
    final int n = geodata.length();
    for (int i = 0; i < n; ++i) {
      final JSONObject person = geodata.getJSONObject(i);
      System.out.println(person.getInt("id"));
      System.out.println(person.getString("name"));
      System.out.println(person.getString("gender"));
      System.out.println(person.getDouble("latitude"));
      System.out.println(person.getDouble("longitude"));
    }
  }
}

输出如下:

C:devscrap>java -cp json.jar;. ShowActivity
1
Julie Sherman
female
37.33774833333334
-121.88670166666667
2
Johnny Depp
male
37.336453
-121.884985

这篇关于在 Java 中解析 JSON 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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