JSON从资产读文件夹不显示 [英] JSON reading from assets folder not displaying

查看:143
本文介绍了JSON从资产读文件夹不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个列表视图从JSON我在文件夹中的资产显示了
由于某种原因,我没有得到ListView中显示的任何东西,它是空的,我使用用于在线JSON codeI同一codeI发前一阵子有几个编辑,活动加载罚款,但没有什么是看过

我的codeS

  @覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_dua);    BuildDrawer();
    RightBuildDrawer();    contactList =新的ArrayList<&HashMap的LT;字符串,字符串>>();    LV =(ListView控件)findViewById(R.id.dua_listview);    新GetContacts()执行();}
公共字符串loadJSONFromAsset(){
    JSON字符串= NULL;
    尝试{        InputStream为= getAssets()开(dua.json);        INT大小= is.​​available();        字节[]缓冲区=新的字节[大小]        is.read(缓冲液);        is.close();        JSON =新的String(缓冲,UTF-8);
    }赶上(IOException异常前){
        ex.printStackTrace();
        返回null;
    }
    返回JSON;}
私有类GetContacts扩展的AsyncTask<太虚,太虚,太虚> {    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
        //显示进度对话框
        pDialog =新ProgressDialog(DuaActivity.this);
        pDialog.setMessage(请稍候...);
        pDialog.setCancelable(假);
        pDialog.show();    }    @覆盖
    保护无效doInBackground(虚空......为arg0){
            尝试{
                JSONObject的jsonObj =新的JSONObject(loadJSONFromAsset());                接触= jsonObj.getJSONArray(杜阿斯);                的for(int i = 0; I< contacts.length();我++){
                    JSONObject的C = contacts.getJSONObject(I)                    字符串ID = c.getString(_ ID);
                    串类= c.getString(类);
                    字符串title = c.getString(TAG_TITLE);
                    阿拉伯的String = c.getString(阿拉伯​​语);
                    字符串翻译= c.getString(TAG_TRANSLATION);
                    字符串音译= c.getString(TAG_TRANSLITERATION);
                    String引用= c.getString(TAG_REFERENCE);                    HashMap的<字符串,字符串>接触=新的HashMap<字符串,字符串>();                    contact.put(TAG_ID,身份证);
                    contact.put(TAG_CATEGORY,类别);
                    contact.put(TAG_TITLE,职称);
                    contact.put(TAG_ARABIC,阿拉伯语);
                    contact.put(TAG_TRANSLATION,翻译);
                    contact.put(TAG_TRANSLITERATION,音译);
                    contact.put(TAG_REFERENCE,参考);                    contactList.add(接触);
                }
            }赶上(JSONException E){
                e.printStackTrace();
            }        返回null;
    }    @覆盖
    保护无效onPostExecute(虚空结果){
        super.onPostExecute(结果);
        如果(pDialog.isShowing())
            pDialog.dismiss();
        / **
         *更新解析JSON数据到ListView控件
         * * /        适配器2 =新SimpleAdapter(
                DuaActivity.this,contactList,
                R.layout.dua_list_item,新的String [] {TAG_CATEGORY,TAG_TITLE},
                        新的INT [] {R.id.duaName,R.id.duaCategory});        lv.setAdapter(适配器2);
        lv.setOnItemClickListener(新AdapterView.OnItemClickListener(){
            @覆盖
            公共无效onItemClick(适配器视图<>母公司,观景,
                                    INT位置,长的id){
                //此处输入            }
        });
    }}

这是我的JSON

  {
  杜阿斯:[
    {
        _id:1,
        类别:睡眠
        头衔:当醒来
        阿拉伯语:الحمدللهالذيأحيانابعدماأماتناوإليهالنشور
        翻译:赞美是真主谁给了我们生命,他已经引起了我们死了以后,他是唯一的回报,
        音译:Alhamdu lillaahil-latheeahyaanaa ba'da MAAamaatanaa wa'ilayhin-nushoor
        参考:布哈里,参见铝Asqalani,Fathul巴里11 \\ / 113;穆斯林4 \\ / 2083
    },
  ]
}

logcat的:

  2月7日至3日:45:52.531:W / System.err的(14092):org.json.JSONException:预期':'之后?在{字符4?
??
2月7日至3日:45:52.541:W / System.err的(14092):?? ?? ???? D 41ú??一?? S 21? ??:?? ?? [?
??
2月7日至3日:45:52.541:W / System.err的(14092):?? ?? ?? ?? ?? {??
??
2月7日至3日:45:52.541:W / System.err的(14092):?? ?? ?? ?? ?? ?? ?? ?? ????_??ID???? ??:?? ?? 1 ??,??
??
2月7日至3日:45:52.541:W / System.err的(14092):?? ?? ?? ?? ?? ?? ?? ?? ????类别???? ??:?? ????睡觉????,??
??
2月7日至3日:45:52.541:W / System.err的(14092):?? ?? ?? ?? ?? ?? ?? ?? ????标题???? ??:?? ????W¯¯18 H 20ê??ñ?? ?? ??W¯¯一个进口K ??我?? N 10摹?? ?? ??úP 50??,? ?
??
2月7日至3日:45:52.541:W / System.err的(14092):?? ?? ?? ?? ?? ?? ?? ?? ????阿拉伯???? ??:?? ????DR-NER / O ?? DDGP ???? DNQ0PJ#N-RJN'FN'??(N9R / N ?? EN'??#NEN'* NFN'?? HN%PDNJRGP ??'DFO​​Q4OH1O??,??
??
2月7日至3日:45:52.541:W / System.err的(14092):?? ?? ?? ?? ?? ?? ?? ?? ????翻译???? ??:?? ???? P 21 R'一??我?的??ê?? ?? ??我S 13的γδT22 O ?? ?? A 10 L'L'一个27 H ?? ??W¯¯31 H 22 O ?? ?? ??摹??我v 10è?的?? ??ü?的?? ?? L'我? F 14ê?? ??一个16 F的γδT??ê?? R'18 H 20ê?? ?? H 22一?的?? ?? C 15一? ü?? S 13ê??ð?? ??ú?? S 13的γδT22 O ?? ?? D 41我?è?? ?? ??一个N 10 D 41 α·T 12 O 42 18 H 20我-7 M ?? ?? ??我S 13的γδT18 H 20ê?? ?? R'ê·ΔT??ū ?? R'N 10。????,??
??
2月7日至3日:45:52.541:W / System.err的(14092):?? ?? ?? ?? ?? ?? ?? ?? ??的γδT?? R'一个?N 10 S 13 L'我的γδT??ê?? R'一·ΔT??我11 O 39 N 10 ?? ??:?? ????一?? L 18 H 20一-4米?? D 41ù?? ?? L'我?? L ?? L ?? ??一个一个18 H 20我?? L ?? - ?? L'一·ΔT18 H 20ê??ê?? ????一个18 H 20Ÿ?? ??一??一??ñ一??一?? ?? b 13一???? D 41一?? -10米?? ??一??一??'?一个-10米?? ??一??一ŧ?? ??一??ň?一个一个?? ?? ??W¯¯一个????我?? L ??一个??Ÿ18 H 20我?N 10 - ?? N 10ü?的31 H 22 O 12 O 42 R'??,??
??
2月7日至3日:45:52.541:W / System.err的(14092):?? ?? ?? ?? ?? ?? ?? ?? ????参考???? ??:?? ????一?? L ?? - ?? B 13ü进口K 18 H 20一?? R'我??,?? 12 C 16 F ?? ??? ?一个?? L ?? - ?? A 10 S 13 q 20一?? L'一?? N 10我??,?? ?? F 14一·ΔT26 H? ?ü?? L ?? - ?? B 13一?? R'我?? ?? 1 ?? 1 ?? \\ ?? / ?? 1 ?? 1 ?? ?? 3; ?? ??中号??ü?的?? L ??我-10米?? ?? ?? 4 \\ ?? / ?? 2?0?8 ?? 3 ????
??
2月7日至3日:45:52.541:W / System.err的(14092):?? ?? ?? ?? ??} ??
??
2月7日至3日:45:52.541:W / System.err的(14092):?? ?? ??] ??
??
2月7日至3日:45:52.541:W / System.err的(14092):??} ??
2月7日至3日:45:52.541:W / System.err的(14092):在org.json.JSONTokener.syntaxError(JSONTokener.java:450)
2月7日至3日:45:52.541:W / System.err的(14092):在org.json.JSONTokener.readObject(JSONTokener.java:379)
2月7日至3日:45:52.541:W / System.err的(14092):在org.json.JSONTokener.nextValue(JSONTokener.java:100)
2月7日至3日:45:52.541:W / System.err的(14092):在org.json.JSONObject<&初始化GT;(JSONObject.java:155)
2月7日至3日:45:52.541:W / System.err的(14092):在org.json.JSONObject<&初始化GT;(JSONObject.java:172)
2月7日至3日:45:52.541:W / System.err的(14092):在com.sahaab.muslimzone.activity.DuaActivity $ GetContacts.doInBackground(DuaActivity.java:172)
2月7日至3日:45:52.541:W / System.err的(14092):在com.sahaab.muslimzone.activity.DuaActivity $ GetContacts.doInBackground(DuaActivity.java:1)
2月7日至3日:45:52.541:W / System.err的(14092):在android.os.AsyncTask $ 2.call(AsyncTask.java:288)
2月7日至3日:45:52.541:W / System.err的(14092):在java.util.concurrent.FutureTask.run(FutureTask.java:237)
2月7日至3日:45:52.541:W / System.err的(14092):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)
2月7日至3日:45:52.541:W / System.err的(14092):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
2月7日至3日:45:52.541:W / System.err的(14092):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587)
2月7日至3日:45:52.541:W / System.err的(14092):在java.lang.Thread.run(Thread.java:841)


解决方案

您JSON有错了地方一个逗号:

  {
    杜阿斯:[
        {
            _id:1,
            类别:睡眠
            头衔:当醒来
            阿拉伯语:الحمدللهالذيأحيانابعدماأماتناوإليهالنشور
            翻译:赞美是真主谁给了我们生命,他已经引起了我们死了以后,他是唯一的回报,
            音译:Alhamdu lillaahil-latheeahyaanaa ba'da MAAamaatanaa wa'ilayhin-nushoor
            参考:布哈里,参见铝Asqalani,Fathul巴里一百一十三分之一十一;穆斯林二千零八十三分之四
        }< -----这应该不会在这里
    ]
}

这是正确的JSON。

  {
    杜阿斯:[
        {
            _id:1,
            类别:睡眠
            头衔:当醒来
            阿拉伯语:الحمدللهالذيأحيانابعدماأماتناوإليهالنشور
            翻译:赞美是真主谁给了我们生命,他已经引起了我们死了以后,他是唯一的回报,
            音译:Alhamdu lillaahil-latheeahyaanaa ba'da MAAamaatanaa wa'ilayhin-nushoor
            参考:布哈里,参见铝Asqalani,Fathul巴里一百一十三分之一十一;穆斯林二千零八十三分之四
        }
    ]
}

i am building a listview to display from a JSON i got in my assets folder for some reason i dont get anything displayed in the listview and it is empty, i am using the same code i used for a online JSON code i made a while ago with a few edits, the activity loads fine but nothing is seen

my codes

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dua);

    BuildDrawer();
    RightBuildDrawer();

    contactList = new ArrayList<HashMap<String, String>>();  

    lv = (ListView) findViewById(R.id.dua_listview);

    new GetContacts().execute();

}




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

        InputStream is = getAssets().open("dua.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;

}   


private class GetContacts extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(DuaActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {




            try {
                JSONObject jsonObj = new JSONObject(loadJSONFromAsset());

                contacts = jsonObj.getJSONArray("Duas");

                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);

                    String id = c.getString("_id");
                    String category = c.getString("category");
                    String title = c.getString(TAG_TITLE);
                    String arabic = c.getString("arabic");
                    String translation = c.getString(TAG_TRANSLATION);
                    String transliteration = c.getString(TAG_TRANSLITERATION);
                    String reference = c.getString(TAG_REFERENCE);                                            

                    HashMap<String, String> contact = new HashMap<String, String>();

                    contact.put(TAG_ID, id);
                    contact.put(TAG_CATEGORY, category);
                    contact.put(TAG_TITLE, title);
                    contact.put(TAG_ARABIC, arabic);
                    contact.put(TAG_TRANSLATION, translation);
                    contact.put(TAG_TRANSLITERATION, transliteration);
                    contact.put(TAG_REFERENCE, reference);  

                    contactList.add(contact);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();
        /**
         * Updating parsed JSON data into ListView
         * */

        adapter2 = new SimpleAdapter(
                DuaActivity.this, contactList,
                R.layout.dua_list_item, new String[] { TAG_CATEGORY, TAG_TITLE}, 
                        new int[] { R.id.duaName,R.id.duaCategory});

        lv.setAdapter(adapter2);      
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // ENTER HERE

            }
        });
    }

}

this is my JSON

{
  "Duas" : [
    {
        "_id" : 1,
        "category" : "Sleep",
        "title" : "When waking up",
        "arabic" : "الْحَمْدُ للهِ الَّذِي أَحْيَانَا بَعْدَ مَا أَمَاتَنَا وَإِلَيْهِ النُّشُورُ",
        "translation" : "Praise is to Allah Who gives us life after He has caused us to die and to Him is the return.",
        "transliteration" : "Alhamdu lillaahil-lathee 'ahyaanaa ba'da maa 'amaatanaa wa'ilayhin-nushoor",
        "reference" : "Al-Bukhari, cf. Al-Asqalani, Fathul-Bari 11\/113; Muslim 4\/2083"
    },
  ]
}

logcat ::

07-03 02:45:52.531: W/System.err(14092): org.json.JSONException: Expected ':' after ?? at character 4 of {??
??
07-03 02:45:52.541: W/System.err(14092): ?? ?? ??"??D??u??a??s??"?? ??:?? ??[??
??
07-03 02:45:52.541: W/System.err(14092): ?? ?? ?? ?? ??{??
??
07-03 02:45:52.541: W/System.err(14092): ?? ?? ?? ?? ?? ?? ?? ?? ??"??_??i??d??"?? ??:?? ??1??,??
??
07-03 02:45:52.541: W/System.err(14092): ?? ?? ?? ?? ?? ?? ?? ?? ??"??c??a??t??e??g??o??r??y??"?? ??:?? ??"??S??l??e??e??p??"??,??
??
07-03 02:45:52.541: W/System.err(14092): ?? ?? ?? ?? ?? ?? ?? ?? ??"??t??i??t??l??e??"?? ??:?? ??"??W??h??e??n?? ??w??a??k??i??n??g?? ??u??p??"??,??
??
07-03 02:45:52.541: W/System.err(14092): ?? ?? ?? ?? ?? ?? ?? ?? ??"??a??r??a??b??i??c??"?? ??:?? ??"??'DR-NER/O ??DDGP ??'DNQ0PJ ??#N-RJN'FN' ??(N9R/N ??EN' ??#NEN'*NFN' ??HN%PDNJRGP ??'DFOQ4OH1O"??,??
??
07-03 02:45:52.541: W/System.err(14092): ?? ?? ?? ?? ?? ?? ?? ?? ??"??t??r??a??n??s??l??a??t??i??o??n??"?? ??:?? ??"??P??r??a??i??s??e?? ??i??s?? ??t??o?? ??A??l??l??a??h?? ??W??h??o?? ??g??i??v??e??s?? ??u??s?? ??l??i??f??e?? ??a??f??t??e??r?? ??H??e?? ??h??a??s?? ??c??a??u??s??e??d?? ??u??s?? ??t??o?? ??d??i??e?? ??a??n??d?? ??t??o?? ??H??i??m?? ??i??s?? ??t??h??e?? ??r??e??t??u??r??n??.??"??,??
??
07-03 02:45:52.541: W/System.err(14092): ?? ?? ?? ?? ?? ?? ?? ?? ??"??t??r??a??n??s??l??i??t??e??r??a??t??i??o??n??"?? ??:?? ??"??A??l??h??a??m??d??u?? ??l??i??l??l??a??a??h??i??l??-??l??a??t??h??e??e?? ??'??a??h??y??a??a??n??a??a?? ??b??a??'??d??a?? ??m??a??a?? ??'??a??m??a??a??t??a??n??a??a?? ??w??a??'??i??l??a??y??h??i??n??-??n??u??s??h??o??o??r??"??,??
??
07-03 02:45:52.541: W/System.err(14092): ?? ?? ?? ?? ?? ?? ?? ?? ??"??r??e??f??e??r??e??n??c??e??"?? ??:?? ??"??A??l??-??B??u??k??h??a??r??i??,?? ??c??f??.?? ??A??l??-??A??s??q??a??l??a??n??i??,?? ??F??a??t??h??u??l??-??B??a??r??i?? ??1??1??\??/??1??1??3??;?? ??M??u??s??l??i??m?? ??4??\??/??2??0??8??3??"??
??
07-03 02:45:52.541: W/System.err(14092): ?? ?? ?? ?? ??}??
??
07-03 02:45:52.541: W/System.err(14092): ?? ?? ??]??
??
07-03 02:45:52.541: W/System.err(14092): ??}??
07-03 02:45:52.541: W/System.err(14092):    at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
07-03 02:45:52.541: W/System.err(14092):    at org.json.JSONTokener.readObject(JSONTokener.java:379)
07-03 02:45:52.541: W/System.err(14092):    at org.json.JSONTokener.nextValue(JSONTokener.java:100)
07-03 02:45:52.541: W/System.err(14092):    at org.json.JSONObject.<init>(JSONObject.java:155)
07-03 02:45:52.541: W/System.err(14092):    at org.json.JSONObject.<init>(JSONObject.java:172)
07-03 02:45:52.541: W/System.err(14092):    at com.sahaab.muslimzone.activity.DuaActivity$GetContacts.doInBackground(DuaActivity.java:172)
07-03 02:45:52.541: W/System.err(14092):    at com.sahaab.muslimzone.activity.DuaActivity$GetContacts.doInBackground(DuaActivity.java:1)
07-03 02:45:52.541: W/System.err(14092):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-03 02:45:52.541: W/System.err(14092):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-03 02:45:52.541: W/System.err(14092):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-03 02:45:52.541: W/System.err(14092):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-03 02:45:52.541: W/System.err(14092):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-03 02:45:52.541: W/System.err(14092):    at java.lang.Thread.run(Thread.java:841)

解决方案

Your JSON has a comma in the wrong place:

{
    "Duas": [
        {
            "_id": 1,
            "category": "Sleep",
            "title": "When waking up",
            "arabic": "الْحَمْدُ للهِ الَّذِي أَحْيَانَا بَعْدَ مَا أَمَاتَنَا وَإِلَيْهِ النُّشُورُ",
            "translation": "Praise is to Allah Who gives us life after He has caused us to die and to Him is the return.",
            "transliteration": "Alhamdu lillaahil-lathee 'ahyaanaa ba'da maa 'amaatanaa wa'ilayhin-nushoor",
            "reference": "Al-Bukhari, cf. Al-Asqalani, Fathul-Bari 11/113; Muslim 4/2083"
        }, <----- this should not be here
    ]
}

This is the correct JSON.

{
    "Duas": [
        {
            "_id": 1,
            "category": "Sleep",
            "title": "When waking up",
            "arabic": "الْحَمْدُ للهِ الَّذِي أَحْيَانَا بَعْدَ مَا أَمَاتَنَا وَإِلَيْهِ النُّشُورُ",
            "translation": "Praise is to Allah Who gives us life after He has caused us to die and to Him is the return.",
            "transliteration": "Alhamdu lillaahil-lathee 'ahyaanaa ba'da maa 'amaatanaa wa'ilayhin-nushoor",
            "reference": "Al-Bukhari, cf. Al-Asqalani, Fathul-Bari 11/113; Muslim 4/2083"
        }
    ]
}

这篇关于JSON从资产读文件夹不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆