Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从JSONArray获取字符串_Java_Android - Fatal编程技术网

Java 从JSONArray获取字符串

Java 从JSONArray获取字符串,java,android,Java,Android,我有一个JSONArray,它存储以下内容: 如何在JSONArray中获取字符串?我尝试jaImage.getString(0)但java.lang.NullPointerException发生异常。 下面是我的代码: for(int i = 0; i < jsonArrayStore.length(); i++) { try { JSONArray jaImage = jsonObjectStore[i].getJSONArray("images")

我有一个JSONArray,它存储以下内容:


如何在JSONArray中获取字符串?我尝试
jaImage.getString(0)
java.lang.NullPointerException发生异常。

下面是我的代码:

for(int i = 0; i < jsonArrayStore.length(); i++)
{
    try 
    {
        JSONArray jaImage = jsonObjectStore[i].getJSONArray("images");
        if(jaImage == null)
        {
            Log.v("jaImage", "null");
        }
        else
            Log.v("jaImage", jaImage.toString());
        imgName[i] = new String[jaImage.length()];
        Bitmap sto_image;

        for (int j = 0; j < jaImage.length(); j++) 
        {
            String image = jaImage.getString(j);
            Log.v("jaImage", image);
        }

    } 
    catch (Exception e) 
    {
        Log.v("JSONArray jaImage Error", e.toString());
    }
}

你发布的代码看起来不错。您确定
NullPointerException
发生在
jaImage.getString(0)
行上吗?当您仅使用
toString()
输出一个
异常
时,您将丢失回溯,这对调试非常有帮助。那么我该怎么办呢@DanGetz@AhYong,在catch块中使用类似于Log.v(“Tag”,“JSONArray jaImage Error”,e)的内容
打印stacktrace…非常感谢,现在我终于找到了异常发生的位置,它位于
imgName[I]=newstring[jaImage.length()
@AbishekManoharanIs您的
imgName
2维数组???您是否使用类似于
imgName=newstring[jsonArrayStore.length()]的东西初始化了它
for(int i = 0; i < jsonArrayStore.length(); i++)
{
    try 
    {
        JSONArray jaImage = jsonObjectStore[i].getJSONArray("images");
        if(jaImage == null)
        {
            Log.v("jaImage", "null");
        }
        else
            Log.v("jaImage", jaImage.toString());
        imgName[i] = new String[jaImage.length()];
        Bitmap sto_image;

        for (int j = 0; j < jaImage.length(); j++) 
        {
            String image = jaImage.getString(j);
            Log.v("jaImage", image);
        }

    } 
    catch (Exception e) 
    {
        Log.v("JSONArray jaImage Error", e.toString());
    }
}
06-29 03:02:48.301  24594-24618/mobile.howddy.com.howddymobile V/jaImage﹕ ["7d92a26dddcbd95c876479cf97afa404.jpg\/500\/500\/0","2efb1955602498c0a82fd5c6f9611d0a.png\/500\/500\/0","53633e49bc1a7725cf28cdbfbcd3f03b.jpg\/500\/500\/0"]
06-29 03:02:48.301  24594-24618/mobile.howddy.com.howddymobile V/JSONArray jaImage Error﹕ java.lang.NullPointerException
06-29 03:02:48.301  24594-24618/mobile.howddy.com.howddymobile V/jaImage﹕ ["29778c73baca5d56d5a42c510cc5ae2e.png\/500\/500\/0","a35493d3aa674000566bfc3ef1053fcb.jpg\/500\/500\/0","a5fce41943837ba92f01a0b47aeb1054.jpg\/500\/500\/0"]
06-29 03:02:48.301  24594-24618/mobile.howddy.com.howddymobile V/JSONArray jaImage Error﹕ java.lang.NullPointerException
06-29 03:02:48.301  24594-24618/mobile.howddy.com.howddymobile V/jaImage﹕ ["e308cc58d082906d936a23eae4bc4708.jpg\/500\/500\/0","ab9ce8cadcc379582210d4b65bda9ef7.jpg\/500\/500\/0","867a82d768deff21dfcb9cd988a26d58.jpg\/500\/500\/0","72df45688fe4a70b6c0b1db2d2a2533e.jpg\/500\/500\/0"]
06-29 03:02:48.301  24594-24618/mobile.howddy.com.howddymobile V/JSONArray jaImage Error﹕ java.lang.NullPointerException