gpt4 book ai didi

php - 如何在android java中获取关联数组元素?

转载 作者:行者123 更新时间:2023-11-29 13:11:02 25 4
gpt4 key购买 nike

我正在尝试从 php 中的 mysql 数据库获取数组的值,并在 android Activity 中显示这些值。

PHP 代码:

这里我的数组有两个值,比如 $j[] 有 1 和 2。

    for($y=0;$y<2;$y++){
$val2 ="t".$y;

$set = mysql_query("SELECT col3 FROM table WHERE col2 ='size' AND col1 ='".$j[$val2]."'",$con); //col1 has two value hence given in for loop
$res= mysql_fetch_array($set); //it displays two size value for each col1
$a1[]="size".$y;
$r1[] = $res['col3'];

$set2 = mysql_query("SELECT col3 FROM table WHERE col2 ='price' AND col1 ='".$l[$val2]."'",$con); // same like above
$res2= mysql_fetch_array($set2);
$a2[]="price".$y;
$r2[] = $res2['meta_value'];
}
$h=array();
for($z=0;$z<$y;$z++){
$h[]=array($a1[$z] => $r1[$z], $a2[$z] => $r2[$z]); // storing values of size and price in associative array
$check='1';
}
$r=$h[];
if($check==NULL)
{
$r[$num_rows]="Record is not available";
print(json_encode($r));
}
else
{
$r[$num_rows]="success";
print(json_encode($r));

}

以及运行此 php 页面时的输出,

    [[{"size0":"14","price0":"300"},{"size1":"18","price1":"350"}],"success"]

我的 Android Activity 是,

    String result = null;
InputStream is = null;

try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(link);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

Log.e("log_tag", "connection success ");
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();

}
//convert response to string
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();

result=sb.toString();
Log.e("log_tag", result);
}
catch(Exception e)
{
Log.e("log_tag", "Error converting result "+e.toString());
Toast.makeText(getApplicationContext(), " Input reading fail", Toast.LENGTH_SHORT).show();

}

try
{
JSONArray jArray = new JSONArray(result);

String re=jArray.getString(jArray.length()-1);

int flag=1;

for(int i=0;i<jArray.length()-1;i++)

{
JSONObject json_data = jArray.getJSONObject(i);

Log.i("log_tag2","title: "+jArray['price0']);
}
}
catch(JSONException e)
{
Log.e("log_tag", "Error parsing data "+e.toString());
Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
}

但是我收到错误,错误解析数据 org.json.JSONException:类型 org.json.JSONArray 的值无法转换为 JSONObject

我不知道如何获得值(value)?

最佳答案

for(int i=0;i<jArray.length();i++) { 
JSONArray jArrayInner = jArray.optJSONArray(i);
if (jArrayInner != null) {
for(int j=0;j<jArrayInner.length();j++) {
JSONObject json_data = jArrayInner.getJSONObject(j);
Log.i("log_tag2","title: "+json_data.optString("price0"));
Log.i("log_tag2","title: "+json_data.optString("size0"));
}
}
}

}

要从内部 json 对象检索数据(即从数组中正确检索数据),必须使用 optString (opt*Type) 方法。此外,数组中的每个内部对象都应该具有相同的键(pricesize)。

关于php - 如何在android java中获取关联数组元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22096713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com