仅将嵌套在另一个 json 数组中的 json 数组中的一个值解析为 android
Parse Only one Value from json array which is nested inside another json array into android
美好的一天guys.Im 在一家公司工作,我的大学寄给我一些 json 数组,但根据要求,json 数组必须是下一个
...some array code here
//Here i give the main array name as data but i don't need to post that part
Array
(
[0] => Array
(
[id_] => 30340428
[number] => 10001
[mobile] =>> 10001
[mobile] =0712200096
[name] => 0
[sms_body] => Hello World
[date] => 2014-10-06 17:09:52
)
[1] => Array
(
[id_] => 5782398452
[number] => 3246436
[mobile] => 8727266262
[name] => mynam
[sms_body] => Yollalahihu
[date] => 2014-10-06 17:09:52
)
)
如您所见,根据我的 android 程序的要求,有两个数组,每个数组都具有相同的键名但具有不同的值。
这是我将此 json 数组解析为 android
的代码
JSONObject jsonResponse = new JSONObject(theresultstring);
JSONArray jsonMainNode = jsonResponse.optJSONArray("data");//this is the main array header key name which i didn't posted in array by this I'm able to get into my array
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String number = jsonChildNode.optString("id");
String name = jsonChildNode.optString("number");
String resume = jsonChildNode.optString("mobile");
String address = jsonChildNode.optString("name");
一切正常,除了一个 thing.I 搜索了整个实习但没有解决方案。我只想解析例如第二个数组的值!!!!用我上面的代码当我解析时可以说名称的值我得到其他两个 "0"
和 "mynam"
但是此时我只想获取一个值让我们说第一个名称值而不是第二个数组名称值我该怎么做???我只能理解它的逻辑,我试图遍历 json array 我尝试使用 while 语句 我尝试了几乎所有的东西 nothgin 我只需要从这两个数组中获取一个值而不是获取 "name"
的两个值 如何使用这样的 json 数组代码? ?
你问的问题可能有多种解决方案(至少我是这样理解的)。
如果你只想解析第一个数组,那么这样做
JSONObject jsonResponse = new JSONObject(theresultstring);
JSONArray jsonMainNode = jsonResponse.optJSONArray("data");//this is the main array header key name which i didn't posted in array by this I'm able to get into my array
JSONObject jsonChildNode = jsonMainNode.getJSONObject(0);
String number = jsonChildNode.optString("id");
String name = jsonChildNode.optString("number");
String resume = jsonChildNode.optString("mobile");
String address = jsonChildNode.optString("name");
如果你只想解析第二个数组,那么这样做
JSONObject jsonResponse = new JSONObject(theresultstring);
JSONArray jsonMainNode = jsonResponse.optJSONArray("data");//this is the main array header key name which i didn't posted in array by this I'm able to get into my array
JSONObject jsonChildNode = jsonMainNode.getJSONObject(1);
...rest of the code...
如果您的 json 数组长度不固定(大于或小于 2)并且您只需要第一个数组,那么第一个解决方案仍然有效。但是如果你想要最后一个数组然后这样做
JSONObject jsonResponse = new JSONObject(theresultstring);
JSONArray jsonMainNode = jsonResponse.optJSONArray("data");//this is the main array header key name which i didn't posted in array by this I'm able to get into my array
JSONObject jsonChildNode = jsonMainNode.getJSONObject(jsonMainNode.length() - 1);
...rest of the code...
此外,在此处添加空检查也是一件好事。
美好的一天guys.Im 在一家公司工作,我的大学寄给我一些 json 数组,但根据要求,json 数组必须是下一个
...some array code here
//Here i give the main array name as data but i don't need to post that part
Array
(
[0] => Array
(
[id_] => 30340428
[number] => 10001
[mobile] =>> 10001
[mobile] =0712200096
[name] => 0
[sms_body] => Hello World
[date] => 2014-10-06 17:09:52
)
[1] => Array
(
[id_] => 5782398452
[number] => 3246436
[mobile] => 8727266262
[name] => mynam
[sms_body] => Yollalahihu
[date] => 2014-10-06 17:09:52
)
)
如您所见,根据我的 android 程序的要求,有两个数组,每个数组都具有相同的键名但具有不同的值。 这是我将此 json 数组解析为 android
的代码 JSONObject jsonResponse = new JSONObject(theresultstring);
JSONArray jsonMainNode = jsonResponse.optJSONArray("data");//this is the main array header key name which i didn't posted in array by this I'm able to get into my array
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String number = jsonChildNode.optString("id");
String name = jsonChildNode.optString("number");
String resume = jsonChildNode.optString("mobile");
String address = jsonChildNode.optString("name");
一切正常,除了一个 thing.I 搜索了整个实习但没有解决方案。我只想解析例如第二个数组的值!!!!用我上面的代码当我解析时可以说名称的值我得到其他两个 "0"
和 "mynam"
但是此时我只想获取一个值让我们说第一个名称值而不是第二个数组名称值我该怎么做???我只能理解它的逻辑,我试图遍历 json array 我尝试使用 while 语句 我尝试了几乎所有的东西 nothgin 我只需要从这两个数组中获取一个值而不是获取 "name"
的两个值 如何使用这样的 json 数组代码? ?
你问的问题可能有多种解决方案(至少我是这样理解的)。
如果你只想解析第一个数组,那么这样做
JSONObject jsonResponse = new JSONObject(theresultstring);
JSONArray jsonMainNode = jsonResponse.optJSONArray("data");//this is the main array header key name which i didn't posted in array by this I'm able to get into my array
JSONObject jsonChildNode = jsonMainNode.getJSONObject(0);
String number = jsonChildNode.optString("id");
String name = jsonChildNode.optString("number");
String resume = jsonChildNode.optString("mobile");
String address = jsonChildNode.optString("name");
如果你只想解析第二个数组,那么这样做
JSONObject jsonResponse = new JSONObject(theresultstring);
JSONArray jsonMainNode = jsonResponse.optJSONArray("data");//this is the main array header key name which i didn't posted in array by this I'm able to get into my array
JSONObject jsonChildNode = jsonMainNode.getJSONObject(1);
...rest of the code...
如果您的 json 数组长度不固定(大于或小于 2)并且您只需要第一个数组,那么第一个解决方案仍然有效。但是如果你想要最后一个数组然后这样做
JSONObject jsonResponse = new JSONObject(theresultstring);
JSONArray jsonMainNode = jsonResponse.optJSONArray("data");//this is the main array header key name which i didn't posted in array by this I'm able to get into my array
JSONObject jsonChildNode = jsonMainNode.getJSONObject(jsonMainNode.length() - 1);
...rest of the code...
此外,在此处添加空检查也是一件好事。