本文介绍了JSONException在Android中,如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的以下JSON字符串,我想JSON数组为单独的字符串中使用Android的提取,但我面对JSON例外。

I have this following JSON string and i want extract the json array into separate string using android but i facing JSON exception.

       try
       {


         String str="[{'Festivals':{'Fesa1':'english is good ','Fesb1':'my english is not so accurate','Fesa2':'oriya is very nice language','Fesb2':'my oriya is absolutely good','Fesa3':'cricket is a very popular game ','Fesb3':'cricket is life for indian','Fesa4':'hockey is the original game for india','Fesb4':'hockey needs to improve ','Fesa5':'computer is a very functional device','Fesb5':'computer is very helpful to mankind'}}] ";

         JSONArray jsonArray = new JSONArray(str);

         String abc1=null;

         String abc2=null;

         String abc3=null;

         int i2=0;

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

             JSONObject e = jsonArray.getJSONObject(i);

             JSONObject jsonObject = e.getJSONObject("Festivals");

             tt.setText(tt.getText().toString()+String.valueOf("Total Array Length: "+jsonArray.length()+"\n"));

             for(int i1=0;i1<jsonObject.length();i1++)
             {

                 i2=i1+1;

                abc1=jsonObject.getString("Fesa"+i2);

                abc2=jsonObject.getString("Fesb"+i2);


                tt.setText(tt.getText().toString()+String.valueOf(abc1+" - "+abc2+" - "+" \n"));

            }





         }



    }
    catch (JSONException e)
    {

        tt.setText("JSON Exception");


    }

所以,我收到JSONExcepiton错误。请帮助我。请建议我一些解决方案

So , i am getting JSONExcepiton error. Please help me out. Please suggest me some solution

先谢谢了!

推荐答案

您的JSON是无效的。检查@

Your json is not valid. Check it @ http://jsonlint.com/

这应该是

[   // json array node
    {  // json object node
        "abc": { // json object abc
            "name": "andy",
            "blood": "o+",
            "ge": 45
        }
    }
]

解析

JSONArray jarray = new JSONArray("your jsonstring");
for(int i=0;i<jarray.length();i++)
{
JSONObject jb = (JSONObject)jarray.get(i);
JSONObject jb1 = jb.getJSONObject("abc");
String name = jb1.getString("name");
// similarly blood and use getInt for ge
}

编辑:

更改您的JSON下,使用下面你会得到所有值

Change your json to below and use the below you will get all values

String jsonstring ="{"
        +"abc"+": ["
            +"{"
               + "Fesa"+":"+ "englishisgood"
            +"},"
            +"{"
                +"Fesa"+":"+ "myenglishisnotsoaccurate"
            +"},"
             +"{"
               + "Fesa"+":"+ "germanisverynicelanguage"
            +"}"+","
            +"{"
                +"Fesa"+":"+ "mygermanisabsolutelygood"
            +"},"
            +"{"
                +"Fesa"+":"+ "cricketisaverypopulargame"
            +"},"
            +"{"
                +"Fesa"+":"+ "cricketislifeforindian"
            +"},"
            +"{"
                +"Fesa"+":"+  "hockeyistheoriginalgameforindia"
            +"},"
            +"{"
                +"Fesa"+":"+  "hockeyneedstoimprove"
            +"},"
            +"{"
                +"Fesa"+":"+  "computerisaveryfunctionaldevice"
           +"},"
           +"{"
                +"Fesa"+":"+  "computerisveryhelpfultomankind"
            +"},"
        +"]"+
    "}";

解析

    try
    {
    JSONObject jsb = new JSONObject(jsonstring);
    JSONArray jarray =jsb.getJSONArray("abc");
    for(int i=0;i<jarray.length();i++)
    {
    JSONObject jb = (JSONObject)jarray.get(i);
    String name = jb.getString("Fesa");
    Log.i("..........",name);
    Toast.makeText(getApplicationContext(), name, 1000).show();
    }
    }catch(Exception e)
    {
        e.printStackTrace();
    }

这篇关于JSONException在Android中,如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:08