热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

AndroidJson数据解析是''[]'',数据解析失败-AndroidJsondataparsingis''[]'',Dataparsingfailed

Recently,Itriedtocodealistoflinkman.Iwanttoobtaininglocaldatafile(City.json)andpars

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 setData(String str){
        List 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 

this my context of logcat

这是我的logcat上下文

enter image description here

3 个解决方案

#1


0  

You should go with following code :

你应该使用以下代码:

JSONObject jobj  = new JSONObject(str);

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

    for (int i = 0; i 

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 

#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方法你是否正确使用?啊哈...


推荐阅读
author-avatar
dreamingsue
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有