我正在尝试从php中的mysql数据库中获取数组的值,并在android活动中显示它们。
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活动是,
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
我不知道如何获得价值?
答案 0 :(得分:1)
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)方法。此外,您应该为数组中的每个内部对象(price
和size
)使用相同的键。
答案 1 :(得分:0)
从JSON数组中获取数据,例如:
for(int index = 0; index < jArray.length(); index++)
{
String size0 = jArray.getJSONObject(index).getString("size0");
String price0 = jArray.getJSONObject(index).getString("price0");
}