Android Json数据解析是''[]'',数据解析失败

时间:2022-02-22 15:34:09

Recently, I tried to code a list of linkman. I want to obtaining local data file(City.json) and parsing into listView. However ,the data from JsonObject always null. Help me please. I'm a Newbie. Thanks in advance. the code under:

最近,我试图编写一个链接列表。我想获取本地数据文件(City.json)并解析为listView。但是,JsonObject的数据始终为null。请帮帮我。我是新手。提前致谢。代码在:

City.json

{
//    "state": 1,
    "datas": [
        {
            "id": "820",
            "name": "安阳",
            "sortKey": "A"
        },
        {
            "id": "68",
            "name": "安庆",
            "sortKey": "A"
        },
        {
            "id": "1269",
            "name": "鞍山",
            "sortKey": "A"
        },
        {
            "id": "22",
            "name": "蚌埠",
            "sortKey": "B"
        },
        {
            "id": "1372",
            "name": "包头",
            "sortKey": "B"
        },
        {
            "id": "2419",
            "name": "北京",
            "sortKey": "B"
        },
        {
            "id": "649",
            "name": "保定",
            "sortKey": "B"
        },
        {
            "id": "1492",
            "name": "宝鸡",
            "sortKey": "B"
        },
        {
            "id": "2419",
            "name": "北京",
            "sortKey": "B"
        },
        {
            "id": "649",
            "name": "保定",
            "sortKey": "B"
        },
        {
            "id": "1492",
            "name": "宝鸡",
            "sortKey": "B"
        },
        {
            "id": "2419",
            "name": "北京",
            "sortKey": "B"
        },
        {
            "id": "649",
            "name": "保定",
            "sortKey": "B"
        },
        {
            "id": "1492",
            "name": "宝鸡",
            "sortKey": "B"
        },
        {
            "id": "2419",
            "name": "北京",
            "sortKey": "B"
        },
        {
            "id": "649",
            "name": "保定",
            "sortKey": "B"
        },
        {
            "id": "1492",
            "name": "宝鸡",
            "sortKey": "B"
        }
    ]

}

AppFileReader.java

package me.sitinglin.administrator.wecharlinkmantest;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

/**
 * Created by Administrator on 2016/10/12.
 */

public class AppJsonFileReader {

    public static String getJson(Context context, String fileName){
        StringBuilder builder = new StringBuilder();
        AssetManager manager = context.getAssets();
        try {
            InputStream stream = manager.open(fileName);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
            String line = null;
            while((line = bufferedReader.readLine())!=null){
                builder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
//        Log.i("abc", builder.toString()); 
        return builder.toString();
    }
    public static List<City> setData(String str){
        List<City> list = new ArrayList<>();
        City city ;
        try {
            JSONObject result = new JSONObject(str);
            JSONArray array = result.getJSONArray("datas");
//            JSONArray array =new JSONArray(result);

            int len = array.length();
            Log.i("len", array.toString());
            for (int i = 0; i <len ; i++) {
                JSONObject object = array.optJSONObject(i);
                city = new City();
                city.setId(object.optString("id"));
                city.setName(object.optString("name"));
                city.setSortKey(object.optString("sortKey"));
                list.add(city);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.i("lll", list.toString());
        return list;
    }
}

this my context of logcat

这是我的logcat上下文

Android Json数据解析是''[]'',数据解析失败

3 个解决方案

#1


0  

You should go with following code :

你应该使用以下代码:

JSONObject jobj  = new JSONObject(str);

if(jobj.has("datas")){    
    JSONArray jsonArray = jobj.getJSONArray("datas");
    List<City> list = new ArrayList<>();

    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jdataObj = jsonArray.getJSONObject(i);    
        City city = new City();
        city.setId(jdataObj.getString("id"));
        city.setName(jdataObj.getString("name"));
        city.setSortKey(jdataObj.getString("sortKey"));
        list.add(city);
    }
} else {
    Log.e("Json","Json has no datas key.")
}

Hope this will help you.

希望这会帮助你。

#2


1  

Try this:

try {
    JSONObject result = new JSONObject(str); 

    JSONArray jsonArray = result.getJSONArray("datas");

    for (int i = 0; i < jsonArray.length(); i++) {

        JSONObject jsonObject2 = jsonArray.getJSONObject(i);
        city = new City();
        city.setId(object.optString("id"));
        city.setName(object.optString("name"));
        city.setSortKey(object.optString("sortKey"));
        list.add(city);
    }
} catch (JSONException e) {
    e.printStackTrace();
}
Log.i("lll", list.toString());
return list;

#3


0  

I found 3 solution to solve this.i will list 3 things that i've solved below and one of the 3 solutions may helped you. there are three point which one of three point maybe help U : 1. checking out the [local file name] of JSON; 2. checking out variale is "public " or "private"..; 3.checking out some Json method whether you are uesing correct? Aha...

我找到了解决这个问题的3个解决方案。我将列出我在下面解决的3件事,其中3个解决方案可能对你有帮助。有三点可以帮助U点:1。检查JSON的[本地文件名]; 2. check out变量是“公共”或“私人”..; 3.检查一些Json方法你是否正确使用?啊哈...

#1


0  

You should go with following code :

你应该使用以下代码:

JSONObject jobj  = new JSONObject(str);

if(jobj.has("datas")){    
    JSONArray jsonArray = jobj.getJSONArray("datas");
    List<City> list = new ArrayList<>();

    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jdataObj = jsonArray.getJSONObject(i);    
        City city = new City();
        city.setId(jdataObj.getString("id"));
        city.setName(jdataObj.getString("name"));
        city.setSortKey(jdataObj.getString("sortKey"));
        list.add(city);
    }
} else {
    Log.e("Json","Json has no datas key.")
}

Hope this will help you.

希望这会帮助你。

#2


1  

Try this:

try {
    JSONObject result = new JSONObject(str); 

    JSONArray jsonArray = result.getJSONArray("datas");

    for (int i = 0; i < jsonArray.length(); i++) {

        JSONObject jsonObject2 = jsonArray.getJSONObject(i);
        city = new City();
        city.setId(object.optString("id"));
        city.setName(object.optString("name"));
        city.setSortKey(object.optString("sortKey"));
        list.add(city);
    }
} catch (JSONException e) {
    e.printStackTrace();
}
Log.i("lll", list.toString());
return list;

#3


0  

I found 3 solution to solve this.i will list 3 things that i've solved below and one of the 3 solutions may helped you. there are three point which one of three point maybe help U : 1. checking out the [local file name] of JSON; 2. checking out variale is "public " or "private"..; 3.checking out some Json method whether you are uesing correct? Aha...

我找到了解决这个问题的3个解决方案。我将列出我在下面解决的3件事,其中3个解决方案可能对你有帮助。有三点可以帮助U点:1。检查JSON的[本地文件名]; 2. check out变量是“公共”或“私人”..; 3.检查一些Json方法你是否正确使用?啊哈...