gpt4 book ai didi

java - Android JSON解析问题: seems to separate it into 2 separate objects and only recognizes one

转载 作者:行者123 更新时间:2023-12-01 14:46:41 29 4
gpt4 key购买 nike

它的作用:这是一个 Android 应用程序,将模仿我的团队正在创建的网站 apdata.info。这是第二页,它将显示符合他们请求的搜索的机场。

PHP

...

$sql = "SELECT apname,apcity,apstate,apcountry,IATA FROM airports WHERE MATCH(apname,apcity,apstate,apcountry,IATA) AGAINST (? IN BOOLEAN MODE)";
$link = connectToDB();
if($stmt = $link->prepare($sql))
{
$stmt->bind_param('s', $search);

if($stmt->execute())
{
$stmt->store_result();
if($stmt->num_rows >= 1)
{
$resultSet = array();

$stmt->bind_result($apname, $apcity, $apstate, $apcountry, $IATA);
$search_rows = $stmt->num_rows;
if($search_rows > 0){
while($stmt->fetch())
{
$resultSet[] = array('apname' => $apname, 'apcity' => $apcity, 'apstate' => $apstate, 'apcountry' => $apcountry, 'IATA' => $IATA);
//echo "Your search returned $search_rows results";
//echo "<a href='/Airports/airport.php?IATA=".$IATA."'>".$apname."</a><br>";
print(json_encode($resultSet));
...

Java:

private void fetchResults(String searchfor){
String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("search", searchfor));


//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://apdata.info/results/androidresult.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
entity = response.getEntity();
is = entity.getContent();
//System.out.println("------------looks good here");
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//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();
//System.out.println(result);
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}

System.out.println(result);
//parse json data
try{
JSONArray jArray = new JSONArray(result);
System.out.println();
results = new ApResult[jArray.length()];
for(int i=0;i<jArray.length();i++){
System.out.println("-----------jArray.length");
JSONObject json_data = jArray.getJSONObject(i);
results[i] = new ApResult(
json_data.getString("IATA"),
json_data.getString("apname"),
json_data.getString("apcity"),
json_data.getString("apstate"),
json_data.getString("apcountry")
);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}

结果打印出我的结果字符串的结果:

03-13 07:40:06.985: I/System.out(220):  [{"apname":"Bismarck Municipal Airport","apcity":"Bismarck","apstate":"North Dakota","apcountry":"United States","IATA":"BIS"}][{"apname":"Bismarck Municipal 
Airport","apcity":"Bismarck","apstate":"North Dakota","apcountry":"United States","IATA":"BIS"},{"apname":"Hector International Airport","apcity":"Fargo","apstate":"North Dakota","apcountry":"United
States","IATA":"FAR"}][{"apname":"Bismarck Municipal Airport","apcity":"Bismarck","apstate":"North Dakota","apcountry":"United States","IATA":"BIS"},{"apname":"Hector International
Airport","apcity":"Fargo","apstate":"North Dakota","apcountry":"United States","IATA":"FAR"},{"apname":"Cologne Bonn Airport","apcity":"Cologne \/ Bonn","apstate":"North Rhine-Westphalia","apcountry":"Germany","IATA":"CGN"}][{"apname":"Bismarck Municipal Airport","apcity":"Bismarck","apstate":"North
Dakota","apcountry":"United States","IATA":"BIS"},{"apname":"Hector International Airport","apcity":"Fargo","apstate":"North Dakota","apcountry":"United States","IATA":"FAR"},{"apname":"Cologne Bonn Airport","apcity":"Cologne \/ Bonn","apstate":"North Rhine-Westphalia","apcountry":"Germany","IATA":"CGN"},
{"apname":"Dusseldorf International Airport","apcity":"Dusseldorf","apstate":"North Rhine-Westphalia","apcountry":"Germany","IATA":"DUS"}]

问题是,jArray.length = 1,尽管我收到的比这个多得多。正如您从我的输出中看到的,该字符串似乎分为两部分,第一部分是 [{"apname":"俾斯麦市机场","apcity":"俾斯麦","apstate":"北达科他州","apcountry":"美国","IATA":"BIS"}]而其他所有内容均以逗号分隔。

为了解决这个问题,我添加了一个修复此错误的函数:

private String fixBug(String result) {
result = result.replace("][", ",");
return result;
}

但我想知道为什么会发生这种情况。

最佳答案

您不应该为每个结果创建单独的 json。您应该将所有结果包含在一个 json 中。将数据保存在变量中,在 while 循环之外,您可以将其编码为有效的 json。尝试使用http://jsonlint.com/用于验证。

现在你已经

while() {
$resultSet[] = array('apname' => $apname, 'apcity' => $apcity, 'apstate' => $apstate, 'apcountry' => $apcountry, 'IATA' => $IATA);
print(json_encode($resultSet));
}

应该是

while() {
$resultSet[] = array('apname' => $apname, 'apcity' => $apcity, 'apstate' => $apstate, 'apcountry' => $apcountry, 'IATA' => $IATA);
}
print(json_encode($resultSet));

关于java - Android JSON解析问题: seems to separate it into 2 separate objects and only recognizes one,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15380456/

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