获取JSONArray没有数组名? [英] Get JSONArray without array name?

查看:449
本文介绍了获取JSONArray没有数组名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的JSON和尝试本教程: <一href="http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/#comments">http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/#comments

我是新来的JSON,C语言,Java和Android的同时,却在学习。本教程使用什么我打电话了一个名为阵列,但所有的JSON的我要去使用我的Andr​​oid项目将使用简单的表行,没有命名的数组。我使用JSON和教程地震JSON的例子如下。

在经历了地震阵列的教程和迭代使用下述code转换成JAVA HashMap的清单:

  JSONArray地震= json.getJSONArray(地震);
    的for(int i = 0; I&LT; earthquakes.length();我++){
        HashMap的&LT;字符串,字符串&GT;图=新的HashMap&LT;字符串,字符串&GT;();
        JSONObject的E = earthquakes.getJSONObject(我);

        map.put(ID,将String.valueOf(I));
        map.put(名,地​​震的名字:+ e.getString(eqid));
        map.put(大小,幅度:+ e.getString(大小));
        mylist.add(图)
}
 

我的问题是,我该如何使用 json.getJSONArray()如果我的JSON是只是简单的如下?我可以转换的code剩下的,我只需要知道如何使用 getJSONArray(strJsonArrayName)如果我没有加载该JSON strJsonArrayName

我的JSON(未命名的数组)

  [
  {
    CNT:1,
    名:美国,
    峰​​:7
  },
  {
    CNT:2,
    名:名人,
    峰​​:3
  },
  {
    CNT:1,
    名:女,
    峰​​:2
  },
  {
    CNT:1,
    名:语言,
    峰​​:8
  },
  {
    CNT:1,
    名:男,
    峰​​:1
  },
  {
    CNT:1,
    名:地区,
    峰​​:9
  }
]
 

教程的JSON(指定数组)

  {
  地震:[
    {
      eqid:c0001xgp,
      大小:8.8,
      液化天然气:142.369,
      SRC:我们,
      日期时间:2011-03-11 4时46分23秒,
      深度:24.4,
      纬度:38.322
    },
    {
      eqid:c000905e,
      大小:8.6,
      液化天然气:93.0632,
      SRC:我们,
      日期时间:2012-04-11六点38分37秒,
      深度:22.9,
      纬度:2.311
    },
    {
      eqid:2007hear,
      大小:8.4,
      液化天然气:101.3815,
      SRC:我们,
      日期时间:2007-09-12九时十分26秒,
      深度:30,
      纬度: -  4.5172
    },
    {
      eqid:c00090da,
      大小:8.2,
      液化天然气:92.4522,
      SRC:我们,
      日期时间:2012-04-11 8点43分09秒,
      深度:16.4,
      纬度:0.7731
    },
    {
      eqid:2007aqbk,
      大小:8,
      液化天然气:156.9567,
      SRC:我们,
      日期时间:2007-04-01 18点39分56秒,
      深度:10,
      纬度: -  8.4528
    },
    {
      eqid:2007hec6,
      大小:7.8,
      液化天然气:100.9638,
      SRC:我们,
      日期时间:2007-09-12 21点49分01秒,
      深度:10,
      纬度: -  2.5265
    },
    {
      eqid:a00043nx,
      大小:7.7,
      液化天然气:100.1139,
      SRC:我们,
      日期时间:2010-10-25 12点42分22秒,
      深度:20.6,
      纬度: -  3.4841
    },
    {
      eqid:2010utc5,
      大小:7.7,
      液化天然气:97.1315,
      SRC:我们,
      日期时间:2010-04-06 20点十五分02秒,
      深度:31,
      纬度:2.3602
    },
    {
      eqid:2009mebz,
      大小:7.6,
      液化天然气:99.9606,
      SRC:我们,
      日期时间:2009-09-30八时16分〇九秒,
      深度:80,
      纬度: -  0.7889
    },
    {
      eqid:2009kdb2,
      大小:7.6,
      液化天然气:92.9226,
      SRC:我们,
      日期时间:2009-08-10 17时55分39秒,
      深度:33.1,
      纬度:14.0129
    }
  ]
}
 

在本教程中,根据来自@MДΓΓБДLL和@Cody Caughlan的答案,我能够重新格式化JSONFunctions.getJSONFromURL成JSONArray代替的JSONObject。这里是我的修改工作code,谢谢!

 公共类JSONfunctions {
公共静态JSONArray getJSONfromURL(字符串URL){
    InputStream的是= NULL;
    字符串结果=;
    JSONArray jArray = NULL;

            HttpClient的HttpClient的=新DefaultHttpClient();
            HTTPGET HTTPGET =新HTTPGET(URL);
            HTT presponse响应= httpclient.execute(HTTPGET);
            HttpEntity实体= response.getEntity();
            是= entity.getContent();

            的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(就是,ISO-8859-1),8);
            StringBuilder的SB =新的StringBuilder();
            串线= NULL;
            而((行= reader.readLine())!= NULL){
                    sb.append(行+\ N);
            }
            is.close();
            结果= sb.toString();

        jArray =新JSONArray(结果);
    返回jArray;
}
}
 

解决方案

您不需要调用 json.getJSONArray()可言,因为JSON你再有已经工作的的数组。所以,不要建立的JSONObject 的实例;使用 JSONArray 。这应该足够了:

  // ...
JSONArray JSON =新JSONArray(结果);
// ...

的for(int i = 0; I&LT; json.length();我++){
    HashMap的&LT;字符串,字符串&GT;图=新的HashMap&LT;字符串,字符串&GT;();
    JSONObject的E = json.getJSONObject(我);

    map.put(ID,将String.valueOf(I));
    map.put(名,地​​震的名字:+ e.getString(eqid));
    map.put(大小,幅度:+ e.getString(大小));
    mylist.add(图)
}
 

您不能使用完全相同的方法,在本教程中,因为你处理的需求JSON将在根解析成 JSONArray ,不一个的JSONObject

I'm new to JSON and trying this tutorial: http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/#comments

I'm new to JSON, C languages, Java and also Android, but am learning. The tutorial uses what I'm calling a named array, but all of the JSON i'm going to be using in my android project will use simple table rows with no named array. Examples of the JSON i'm using and the earthquake json from the tutorial are below.

The tutorial iterates through the earthquake array and converts into a JAVA hashmap list using the following code:

JSONArray  earthquakes = json.getJSONArray("earthquakes");
    for(int i=0;i<earthquakes.length();i++){                        
        HashMap<String, String> map = new HashMap<String, String>();    
        JSONObject e = earthquakes.getJSONObject(i);

        map.put("id",  String.valueOf(i));
        map.put("name", "Earthquake name:" + e.getString("eqid"));
        map.put("magnitude", "Magnitude: " +  e.getString("magnitude"));
        mylist.add(map);            
}

My question is, how can I use json.getJSONArray("") if my JSON is just simple as below? I can convert the rest of the code, I just need to know how to load that JSON using the getJSONArray("strJsonArrayName") if I don't have a strJsonArrayName.

My JSON (UnNamed Array)

[
  {
    "cnt":1,
    "name":"American",
    "pk":7
  },
  {
    "cnt":2,
    "name":"Celebrities",
    "pk":3
  },
  {
    "cnt":1,
    "name":"Female",
    "pk":2
  },
  {
    "cnt":1,
    "name":"Language",
    "pk":8
  },
  {
    "cnt":1,
    "name":"Male",
    "pk":1
  },
  {
    "cnt":1,
    "name":"Region",
    "pk":9
  }
]

Tutorial's JSON (Named Array)

{
  "earthquakes":[
    {
      "eqid":"c0001xgp",
      "magnitude":8.8,
      "lng":142.369,
      "src":"us",
      "datetime":"2011-03-11 04:46:23",
      "depth":24.4,
      "lat":38.322
    },
    {
      "eqid":"c000905e",
      "magnitude":8.6,
      "lng":93.0632,
      "src":"us",
      "datetime":"2012-04-11 06:38:37",
      "depth":22.9,
      "lat":2.311
    },
    {
      "eqid":"2007hear",
      "magnitude":8.4,
      "lng":101.3815,
      "src":"us",
      "datetime":"2007-09-12 09:10:26",
      "depth":30,
      "lat":-4.5172
    },
    {
      "eqid":"c00090da",
      "magnitude":8.2,
      "lng":92.4522,
      "src":"us",
      "datetime":"2012-04-11 08:43:09",
      "depth":16.4,
      "lat":0.7731
    },
    {
      "eqid":"2007aqbk",
      "magnitude":8,
      "lng":156.9567,
      "src":"us",
      "datetime":"2007-04-01 18:39:56",
      "depth":10,
      "lat":-8.4528
    },
    {
      "eqid":"2007hec6",
      "magnitude":7.8,
      "lng":100.9638,
      "src":"us",
      "datetime":"2007-09-12 21:49:01",
      "depth":10,
      "lat":-2.5265
    },
    {
      "eqid":"a00043nx",
      "magnitude":7.7,
      "lng":100.1139,
      "src":"us",
      "datetime":"2010-10-25 12:42:22",
      "depth":20.6,
      "lat":-3.4841
    },
    {
      "eqid":"2010utc5",
      "magnitude":7.7,
      "lng":97.1315,
      "src":"us",
      "datetime":"2010-04-06 20:15:02",
      "depth":31,
      "lat":2.3602
    },
    {
      "eqid":"2009mebz",
      "magnitude":7.6,
      "lng":99.9606,
      "src":"us",
      "datetime":"2009-09-30 08:16:09",
      "depth":80,
      "lat":-0.7889
    },
    {
      "eqid":"2009kdb2",
      "magnitude":7.6,
      "lng":92.9226,
      "src":"us",
      "datetime":"2009-08-10 17:55:39",
      "depth":33.1,
      "lat":14.0129
    }
  ]
}

In the tutorial, based on the answers from @MДΓΓ БДLL and @Cody Caughlan , I was able to reformat the JSONFunctions.getJSONFromURL into a JSONArray instead of a JSONObject. Here is my modified working code, thank you!

public class JSONfunctions {
public static JSONArray getJSONfromURL(String url){
    InputStream is = null;
    String result = "";
    JSONArray jArray = null;

            HttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(url);
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
            }
            is.close();
            result=sb.toString();

        jArray = new JSONArray(result);            
    return jArray;
}
}

解决方案

You don't need to call json.getJSONArray() at all, because the JSON you're working with already is an array. So, don't construct an instance of JSONObject; use a JSONArray. This should suffice:

// ...
JSONArray json = new JSONArray(result);
// ...

for(int i=0;i<json.length();i++){                        
    HashMap<String, String> map = new HashMap<String, String>();    
    JSONObject e = json.getJSONObject(i);

    map.put("id",  String.valueOf(i));
    map.put("name", "Earthquake name:" + e.getString("eqid"));
    map.put("magnitude", "Magnitude: " +  e.getString("magnitude"));
    mylist.add(map);            
}

You can't use exactly the same methods as in the tutorial, because the JSON you're dealing with needs to be parsed into a JSONArray at the root, not a JSONObject.

这篇关于获取JSONArray没有数组名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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