解析json字符串 java,在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.(JSONObject.java:7)

at ShowActivity.main(ShowActivity.java:29)

My Class looks like-

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.

解决方案

See my comment.

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

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\""

+ " }"

+ " ]"

+ "}";

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"));

}

}

}

Here's the output:

C:\dev\scrap>java -cp json.jar;. ShowActivity

1

Julie Sherman

female

37.33774833333334

-121.88670166666667

2

Johnny Depp

male

37.336453

-121.884985

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值