android - Android中的JSONException,如何解决?

标签 android json

我有以下 JSON 字符串,我想使用 android 将 json 数组提取到单独的字符串中,但我遇到 JSON 异常。

       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 错误。请帮帮我。请给我一些解决方案

提前致谢!!!

最佳答案

您的 json 无效。检查@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 更改为下面并使用下面您将获得所有值

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();
    }

关于android - Android中的JSONException,如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21649650/

相关文章:

java - 将 Android 客户端与 Java 应用服务器连接的最佳方式是什么?

Android 应用程序用户登录 Activity

javascript - 主干 View 未定义

json - 在 Go 中将 Json 数据解码到 map 中

java - 如何在Android中的聊天应用程序中更新新消息的 ListView

安卓 : Callback on insertion of microSD

android - Android checkSelfPermission()中的 "this"指的是什么?

javascript - JSON.parse() 对比。 .json()

python - 根据python中的字典分组删除键值对

javascript - 将第二个值插入 json 数组时属性未定义