[英]how to parse json when it give different type of response
I am parsing data from JSON in Android, but JSON response is behaving strangely. 我正在从Android中的JSON解析数据,但是JSON响应的行为异常。
If it contains more than one data the response is like this 如果包含多个数据,则响应如下
{"e":"701",
"data":[{"id":"121"},
{"id":"122"}
]
}
If it contains only one data the response is like this 如果仅包含一个数据,则响应如下
{"e":"701",
"data":{"1":{"id":"93"}}
}
The code that send JSON response is 发送JSON响应的代码是
$r1=mysql_query($sql1,$con);
$count1=mysql_num_rows($r1);
if($count1>0)
{
while ($row1 = mysql_fetch_assoc($r1)) {
$data1[$i] = $row1 ;
}
}
$c = new Emp();
$c->e = "801";
$c->data =$data1;
echo json_encode($c);
How to parse this in one stretch? 如何一一解析? Any help would be appreciated.
任何帮助,将不胜感激。
How about you add this in the while loop ($e will be a variable which in the case above contains 801) 如何在while循环中添加它($ e将是一个变量,在上述情况下包含801)
$info = array('e' => $e,
'data' => $data1
);
$rows[] = $info;
and then out of the while loop you take it out with json as an array which will give you the result you need if I get the question right. 然后从while循环中将json作为数组取出,如果我正确回答问题,它将为您提供所需的结果。
echo str_replace("\\","", json_encode($rows));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.