在Android上阅读JSON,无输出

时间:2013-09-18 09:36:30

标签: java android json

这是我的JSON数据。

{"JSONDATA":[{"key":0,"value":"--Any--"},{"key":61,"value":"Accounting"},{"key":81,"value":"Aerospace & Defense"},{"key":72,"value":"Automotive"},{"key":83,"value":"Banking"},{"key":84,"value":"Biotech"},{"key":85,"value":"Construction"},{"key":86,"value":"Customer Service"},{"key":87,"value":"Education"},{"key":82,"value":"Energy"},{"key":70,"value":"Finance"},{"key":193,"value":"Government"},{"key":194,"value":"Healthcare"},{"key":71,"value":"Insurance"},{"key":73,"value":"Legal"},{"key":62,"value":"Management"},{"key":63,"value":"Manufacturing"},{"key":64,"value":"Marketing\/Advertising"},{"key":77,"value":"Media - Journalism"},{"key":74,"value":"Pharmaceutical"},{"key":75,"value":"Real Estate"},{"key":76,"value":"Research"},{"key":65,"value":"Restaurant"},{"key":66,"value":"Retail"},{"key":67,"value":"Sales"},{"key":78,"value":"Science"},{"key":68,"value":"Telecommunications"},{"key":79,"value":"Training"},{"key":69,"value":"Transportation"},{"key":80,"value":"Utilities"}]}

我想在我的Android应用程序上解码它,这是我用过的代码。但是我的输出没有任何结果。没有错误。

JSONObject jObject= new JSONObject();
JSONArray menuObject = new JSONArray(jObject.getString("JSONDATA"));
String app;
for (int i = 0; i<menuObject.length(); i++) {
{
 app=menuObject.getJSONObject(i).getString("value").toString();
 a.append(app); // a is my TextView
}

6 个答案:

答案 0 :(得分:4)

首先,您没有使用任何内容初始化jObject

//pass in string
JSONObject jObject= new JSONObject(jsonString);

JSONObject需要解析一些东西,否则(你现在的方式)他们初始化没有数据,这不是很有帮助。

其次,当你真正想要一个数组时,你正在使用getString

JSONArray menuObject = jObject.getJSONArray("JSONDATA");

getString旨在从JSON对象返回一段字符串数据"JSONDATA"拥有数组,因此我们需要选择要检索的正确类型。

第三,您有一个多余的toString(),因为getString已经返回String

app=menuObject.getJSONObject(i).getString("value");

答案 1 :(得分:1)

错了:

JSONArray menuObject = new JSONArray(jObject.getString("JSONDATA"));

尝试:

JSONObject jObject= new JSONObject(yourJSONString);
JSONArray menuObject = jObject.getJSONArray("JSONDATA");

记住一件事: 使用要解析的JSON String创建一个JSON对象,然后可以从创建的JSON对象中获取String / JSON Object或JSON Array。

答案 2 :(得分:0)

将您的json响应存储在String

String jsonResponse="YOUR JSON RESPONSE STRING";

//传递字符串如下

JSONObject jObject= new JSONObject(jsonResponse);
JSONArray menuObject = jObject.getJSONArray("JSONDATA"));
String app;
for (int i = 0; i<menuObject.length(); i++) {
{
 app=menuObject.getJSONObject(i).getString("value").toString();
 a.append(app); // a is my TextView
}

答案 3 :(得分:0)

JSONObjectJSONArray中使用适当的getter和setter,而你的“JSON DATA”条目不是字符串。做这样的事情:

JSONObject jObject = new JSONObject(yourJsonString);
JSONArray menuArray = jObject.getJSONArray("JSONDATA");
for (int i = 0; i < menuArray.length(); i++) {
    String app = menuObject.getJSONObject(i).getString("value");
    a.append(app); // a is my TextView
}

答案 4 :(得分:0)

使用以下代码解析json字符串。

JSONObject obj = new JSONObject(youtString);
JSONArray array = obj.getJSONArray("JSONDATA");
for (int i = 0; i < array.length(); i++) {
  JSONObject c = array.getJSONObject(i);

  String key = c.getString("key");
  String value = c.getString("value");
  a.append(value);
}

答案 5 :(得分:0)

使用: -

String result="[{"key":0,"value":"--Any--"},{"key":61,"value":"Accounting"},{"key":81,"value":"Aerospace & Defense"},{"key":72,"value":"Automotive"},{"key":83,"value":"Banking"},{"key":84,"value":"Biotech"},{"key":85,"value":"Construction"},{"key":86,"value":"Customer Service"},{"key":87,"value":"Education"},{"key":82,"value":"Energy"},{"key":70,"value":"Finance"},{"key":193,"value":"Government"},{"key":194,"value":"Healthcare"},{"key":71,"value":"Insurance"},{"key":73,"value":"Legal"},{"key":62,"value":"Management"},{"key":63,"value":"Manufacturing"},{"key":64,"value":"Marketing\/Advertising"},{"key":77,"value":"Media - Journalism"},{"key":74,"value":"Pharmaceutical"},{"key":75,"value":"Real Estate"},{"key":76,"value":"Research"},{"key":65,"value":"Restaurant"},{"key":66,"value":"Retail"},{"key":67,"value":"Sales"},{"key":78,"value":"Science"},{"key":68,"value":"Telecommunications"},{"key":79,"value":"Training"},{"key":69,"value":"Transportation"},{"key":80,"value":"Utilities"}]";
JSONArray menuObject = new JSONArray(result);
String app;
for (int i = 0; i<menuObject.length(); i++) {
{
 app=menuObject.getJSONObject(i).getString("value").toString();
 a.append(app); // a is my TextView
}