Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
提取Json并保存到Arraylist Android_Android - Fatal编程技术网

提取Json并保存到Arraylist Android

提取Json并保存到Arraylist Android,android,Android,如何提取下面的json并将其保存在arraylist中 { "trains": { "train": [ { "@id": 1000000103, "@version": 1, "@status": "active",

如何提取下面的json并将其保存在arraylist中

   {
            "trains": {
                "train": [
                    {
                        "@id": 1000000103,
                        "@version": 1,
                        "@status": "active",
                        "@name": "dffffff",
                        "@description": "ffffff half of the nineteenth century.",
                        "@city": "fff",
                        "@phone": "+230 595-1454",
                        "@email": "ffffr@mffc.mu",
                        "@website": "www4u",
                        "@latitude": -5.2882,
                        "@longitude": 3.499,
                        "@defaultLocale": "",
                        "@holes": 48,
                        "@par": 72,
                        "@slope": 0,
                        "@distance": 5.005273,
                        "circuits": {
                            "circuit": []
                        },
                        "localizations": {
                            "localization": []
                        }
                    },
                    {
                        "@id": 1000000105,
                        "@version": 1,
                        "@status": "active",
                        "@name": " xClub",
                        "@description": "",
                        "@city": " xlet",
                        "@phone": "+44465\t",
                        "@email": "",
                        "@website": "wweffl.com",
                        "@latitude": -2.040318,
                        "@longitude": 54548,
                        "@defaultLocale": "",
                        "@holes": 18,
                        "@par": 32,
                        "@slope": 0,
                        "@distance": 2441673,
                        "circuits": {
                            "circuit": []
                        },
                        "localizations": {
                            "localization": []
                        }
                    }
                ]
            }

    }
我的工作

try {

                jobj_trouve_train = new JSONObject(reponse_trouve_train);

                String jsonobj = jobj_trouve_golf.getString("trains");
                //String jsonobj1 = jobj_trouve_golf.getString("train");

                //jobj_trouve_train = new JSONObject(reponse_trouve_train);

                //jsonArray = jobj_trouve_golf.getJSONArray("trains");
                //jsonArray= new JSONArray(jsonobj);

                //System.out.println("jsonArray "+jsonArray);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

您可以使用Google开发的
Gson
库将Json解析为
对象
。 参考资料如下: 下面是一个示例:

如果我没有感到困惑,也许这一个是正确的

试试这个

try {

                jobj_trouve_train = new JSONObject(reponse_trouve_train);

                JSONObject jsonobj = jobj_trouve_train.getJSONObject("trains");
                JSONArray jsonobj1 = jsonobj.getJSONArray("train");

          for(int i = 0;i< jsonobj1.length();i++){
                JSONObject jsonj = jsonobj1.getJSONObject(i);

                System.out.println("@id "+jsonj.getString("@id"));
                //   Same for remaining all 
                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block                  

                   e.printStackTrace();
        }
试试看{
jobj_-trouve_列车=新的JSONObject(响应列车);
JSONObject jsonobj=jobj_trouve_train.getJSONObject(“trains”);
JSONArray jsonobj1=jsonobj.getJSONArray(“火车”);
for(int i=0;i
for(int i=0;i }

公共对象void toObject(字符串jsonMsg)抛出JSONException{
JSONObject trains=(JSONObject)新的JSONTokener(jsonMsg).nextValue();
如果(列车有(“列车”)){
JSONArray train=(JSONArray)新的JSONTokener(object.getString(“train”)).nextValue();
for(int t=0;t

--经过测试,我尝试修改代码以匹配您的JSON数据。完成其余部分。

谢谢这一行JSONArray jsonobj1=jsonobj。getJSONArray(“train”);是aligato的解决方案
try {

                jobj_trouve_train = new JSONObject(reponse_trouve_train);

                JSONObject jsonobj = jobj_trouve_train.getJSONObject("trains");
                JSONArray jsonobj1 = jsonobj.getJSONArray("train");

          for(int i = 0;i< jsonobj1.length();i++){
                JSONObject jsonj = jsonobj1.getJSONObject(i);

                System.out.println("@id "+jsonj.getString("@id"));
                //   Same for remaining all 
                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block                  

                   e.printStackTrace();
        }
JSONArray jasonArray = new JSONArray(result.getProperty("trains").toString());
JSONObject jsonObj = jasonArray.optJSONObject(i);
trainObject = new JSONObject(jsonObj.optString("train").toString());
int id = trainObject.optInt("id");

and so on..
public Object void toObject(String jsonMsg) throws JSONException {
    JSONObject trains= (JSONObject) new JSONTokener(jsonMsg).nextValue();
    if(trains.has("trains")){
               JSONArray train = (JSONArray) new JSONTokener(object.getString("train")).nextValue();
                for (int t = 0; t < jsonArray.length(); t++) {
                   String temp = jsonArray.getJSONObject(t).toString();
                   JSONObject object = (JSONObject) new JSONTokener(temp).nextValue();
                   if(object.has("@id"))
                      object.getString("@id");
                   // now similar procedure of reading
                   // read values and save it in an "object"
    }       
    return savedObject;
}