不工作吐司数组值 [英] Toast Array value not working

查看:94
本文介绍了不工作吐司数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图测试,以确保我我想要的值是数组中为止。下面的第一个敬酒的作品,只是吐出我的整个JSON数据的字符串。

I am trying to test to make sure my the value I want is in the array. Below the first toast works and just spits out my whole string of JSON data.

不过,我正努力展现[1]我的新JSONArray的jsonArray是给我下面的错误值第二敬酒 jsonArray不能定义一个变量我这不是正确的引用呢?

But the second toast which I am trying to show the value of [1] in my new JSONArray the jsonArray is giving me the following error jsonArray can not be defined to a variable Am I not referencing it correctly?

try{
    JSONArray jsonArray = new JSONArray(jsonData);
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Toast.makeText(MainActivity.this, jsonData, Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, jsonArray[1], Toast.LENGTH_LONG).show();

修改基于反馈它也可能是一个范围问题,因为它是在尝试我必须用一个试试?如果是的话我怎么做它提供给脚本的其余部分。

EDIT based on the feedback it could also be a scope issue since it is in the "try" Do I have to use a try? and if so how do I make it available to the rest of the script.

推荐答案

JSONArray 只是一个Java对象,而不是一个数组,这意味着你无法在其上使用数组索引。也许你的意思 jsonData [1] ?此外,由于 jsonArray 声明,并在初始化尝试块,它不是在范围为吐司电话。假设 jsonData 收藏

JSONArray is just a Java object, not an array, which means you cannot use array indexing on it. Perhaps you meant jsonData[1]? Additionally, since jsonArray is declared and initialized in the try block, it is not in scope for the Toast call. Assuming that jsonData is a Collection:

try
{
    JSONArray jsonArray = new JSONArray(jsonData);
    // ...
    Toast.makeText(MainActivity.this, jsonData.get(1), Toast.LENGTH_LONG).show();
}
// ...

这篇关于不工作吐司数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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