首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在android中显示Json数据

在android中显示Json数据
EN

Stack Overflow用户
提问于 2016-07-24 19:49:23
回答 1查看 107关注 0票数 0

我正在尝试在安卓应用程序中显示json数据,但我遇到了困难,我认为这可能与json文件的格式有关,因为我想要在headers数组中获取名称和代码的值。而且它不起作用

这是json文件

代码语言:javascript
复制
 {
status: "SUCCESS",
headers: 
{
id: "4",
name: "GLO",
code: "GLO",
background_color_code: "15B709",
text_color_code: "ffffff",
statusMessage: "Hi +234805, an ACCESS FEE of N20.00 will be charged in order to access this Platform"
},
statusMessage: "Movies Loaded successfully",
caption: "Discover"
}

这是javaclass

代码语言:javascript
复制
package com.example.cann;

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONStringer;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;

import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class CategoryActivity extends ListActivity {

    private static final String TAG_POSTS = "headers";
    public static final String TAG_PIC = "name";
    public static final String TAG_NOTE = "code";
    JSONParser jParser;
    private static final String URL_CATEGORY =  "https://dobox.tv/api/home.json?platform=Android&api=1.1";
    private BaseAdapter mAdapter;
    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);
        lv = getListView();
        lv.setDivider(null);

        lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {

                Toast.makeText(CategoryActivity.this, "Item selected: " + position,
                        Toast.LENGTH_LONG).show();

                // Uncomment this to start a new Activity for a chosen item
                /* Intent i = new Intent(getApplicationContext(),
                        ItemListActivity.class);

                String category_id = ((TextView) view
                        .findViewById(R.id.category_id)).getText()
                        .toString();
                i.putExtra("category_id", category_id);

                startActivity(i);*/
            }
        });

                    new LoadComments().execute();
    }

    class LoadComments extends AsyncTask<Void, Void, ArrayList<HashMap<String,String>>> {
        private ProgressDialog pDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(CategoryActivity.this);

            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);

        }

        @Override
        protected ArrayList<HashMap<String, String>> doInBackground(Void... arg0) {

            ArrayList<HashMap<String, String>> categoryList = new ArrayList<HashMap<String, String>>();
            jParser = new JSONParser();

            JSONObject json = jParser.getJSONFromUrl(URL_CATEGORY);

            try {
                  JSONArray categories =json.getJSONArray("headers");
                for (int i = 0; i < categories.length(); i++) {
                    String state = categories.getJSONObject(i).getString("name");
                    String status = categories.getJSONObject(i).getString("code");

                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(TAG_PIC, state);
                    map.put(TAG_NOTE, status);
                    categoryList.add(map);

                }
            }catch (Throwable e){
                e.printStackTrace();
            }
            return categoryList;
        }

        @Override
        protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
            super.onPostExecute(result);
            if(pDialog.isShowing()){
                pDialog.dismiss();
            }
            mAdapter = new CategoryListAdapter(CategoryActivity.this,result);
            lv.setAdapter(mAdapter);
            mAdapter.notifyDataSetChanged();
        }
    }
}

我得到了这个错误

代码语言:javascript
复制
07-24 12:20:50.849  16574-16591/com.example.cann W/System.err? org.json.JSONException: Value  at headers of type java.lang.String cannot be converted to JSONArray
07-24 12:20:50.852  16574-16591/com.example.cann W/System.err? at org.json.JSON.typeMismatch(JSON.java:100)
07-24 12:20:50.852  16574-16591/com.example.cann W/System.err? at org.json.JSONObject.getJSONArray(JSONObject.java:588)
07-24 12:20:50.853  16574-16591/com.example.cann W/System.err? at com.example.cann.CategoryActivity$LoadComments.doInBackground(CategoryActivity.java:82)
07-24 12:20:50.853  16574-16591/com.example.cann W/System.err? at com.example.cann.CategoryActivity$LoadComments.doInBackground(CategoryActivity.java:61)
EN

回答 1

Stack Overflow用户

发布于 2016-07-24 20:01:31

您提供的JSON文件的JSON格式无效。而且“JSONObject”属性包含的是头,而不是JSONArray。当你尝试将JSONObject作为JSONArray获取时,你得到了JSON异常。

你可以试试这个:

代码语言:javascript
复制
{
   "status": "SUCCESS",
   "headers": [
   {
      "id": "4",
      "name": "GLO",
      "code": "GLO",
      "background_color_code": "15B709",
      "text_color_code": "ffffff",
      "statusMessage": "Hi +234805, an ACCESS FEE of N20.00 will be charged in order to access this Platform"
   },
   {
      "id": "5",
      "name": "PLU",
      "code": "PLU",
      "background_color_code": "15B709",
      "text_color_code": "ffffff",
      "statusMessage": "Hi +234805, an ACCESS FEE of N20.00 will be charged in order to access this Platform"
   }
   ],
   "statusMessage": "Movies Loaded successfully",
   "caption": "Discover"
}

请先检查您的JSON数据是否有效。您可以从herehere获得帮助

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38551678

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档