简体   繁体   English

Android中JSON对象的JSON解析

[英]JSON Parsing of JSON Objects in android

I have a JSON like, 我有一个JSON,

 {

 "Area1": "areacode1",
 "Area2": "areacode2",
 "Area3" : "areacode3"

 }

I want to parse the json and iterate "area" it to autocompletetextview, My code is, 我想解析json并将其“区域”迭代为autocompletetextview,我的代码是,

//Reading JSON from local file //从本地文件读取JSON

public String loadJSONFromAsset() {
    String json = null;
    try {

        InputStream is = getAssets().open("arealist.json");

        int size = is.available();

        byte[] buffer = new byte[size];

        is.read(buffer);

        is.close();

        json = new String(buffer, "UTF-8");



    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;

}

//parsing and iterating the json... //解析和迭代json ...

    try {
        JSONObject obj = new JSONObject(loadJSONFromAsset());
           Iterator<String> keys= obj.keys();
        while (keys.hasNext())
        {
            String keyValue = (String)keys.next();
            String valueString = obj.getString(keyValue);
            Log.d("Details-->",valueString);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

I am getting the error as "Type mismatch:can't convert JSONObject to JSONArray",I want to know how to convert JSONObject to string and iterate to List.I am new to android so confused how to proceed further Kindly help,Thanks in advance. 我收到“类型不匹配:无法将JSONObject转换为JSONArray”的错误,我想知道如何将JSONObject转换为字符串并迭代到List。我是android的新手,所以很困惑如何继续进行进一步的工作,谢谢预先。

try to read arealist.json like this 尝试像这样读取arealist.json

InputStream inputStream = getContext().getAssets().open("arealist.json");
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));


 String line;
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null){
                sb.append(line).append("\n");
            }

            //parse JSON and store it in the list
            String jsonString =  sb.toString();

and use this jsonString in JSONobject 并在JSONobject中使用此jsonString

After debug the given json by the below code: 通过以下代码调试给定的json后:

    String json = "{\n" +
            "\n" +
            " \"Area1\": \"areacode1\",\n" +
            "  \"Area2\": \"areacode2\",\n" +
            "  \"Area3\" : \"areacode3\"\n" +
            "\n" +
            " }";
    try {
        JSONObject obj = new JSONObject(json);
        Iterator<String> keys= obj.keys();
        while (keys.hasNext())
        {
            String keyValue = (String)keys.next();
            String valueString = obj.getString(keyValue);
            Log.d("Details-->",valueString);
        }



    } catch (JSONException e) {
        e.printStackTrace();
    }

I realize that the way you read which json does not have any problem. 我意识到您阅读哪个json的方式没有任何问题。 The problem is the string return from loadJSONFromAsset . 问题是从loadJSONFromAsset返回的字符串。 You must correct the out of that method. 您必须更正该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM