从iPhone上的Web服务器获取数据会返回Garbage值

时间:2011-03-21 10:04:43

标签: iphone mysql json

我正在使用此查询使用JSON解析将数据从网络服务器提取到我的iPhone应用程序。

SELECT a.FundID, a.FundName, a.Strike, a.LongShort, a.Current, a.Points, a.OpenClose
FROM tbl_Positions a, tbl_FundStatic b
WHERE b.FundID = a.FundID
AND b.UserID = '14'
AND a.OpenClose != 'Close'
UNION 
SELECT c.FundID, c.FundName, '0' AS Strike, "-" AS LongShort, b.LastTradePrice, '0' AS Points, "-" AS OpenClose
FROM tbl_FundStatic c, tbl_MarketData b
WHERE c.UserID = '14'
AND b.IndexCode = c.`Index` 
AND c.FundID NOT 
IN (
     SELECT DISTINCT (FundID)
     FROM tbl_Positions
   )

理想情况下,它应该返回像

这样的数据

enter image description here

但它显示了列PointsStrike的垃圾值(如“MA ==”等)。

可能有什么不对?

修改

我正在使用SBJSON Parser。

我正在使用以下代码将数据解析为服务器端的JSON字符串:

        DataTable dt = new DataTable();
        MySqlDataAdapter da = new MySqlDataAdapter(cmd);
        da.Fill(dt);
        objMyCon.Close();

        String jsonString = JsonConvert.SerializeObject(dt);
        String finalString = "{\"ExecuteTrade\":";
        finalString += jsonString;
        finalString += "}";

        return finalString; 

finalString值

{"ExecuteTrade":[{"FundID":28,"FundName":"Sam Fund 2","Strike":"MTIxMzA=","LongShort":"Long","Current":11985.00,"Points":"LTE0NQ==","OpenClose":"Open"},
{"FundID":27,"FundName":"Sam Fund 1","Strike":"MTE5ODU=","LongShort":"Long","Current":11985.00,"Points":"NTAwMDA=","OpenClose":"Open"},
{"FundID":32,"FundName":"Sam Fund 3","Strike":"MjIwMDA=","LongShort":"Long","Current":14000.00,"Points":"NjAwMA==","OpenClose":"Open"},
{"FundID":45,"FundName":"Rob Fund test","Strike":"OTk5OQ==","LongShort":"NULL","Current":11984.61,"Points":"OTk5OQ==","OpenClose":"NULL"},   
   {"FundID":46,"FundName":"newtestfund5th","Strike":"OTk5OQ==","LongShort":"NULL","Current":11984.61,"Points":"OTk5OQ==","OpenClose":"NULL"}]}

这是Datatable:

enter image description here

在应用方面我正在使用

NSDictionary *diction = [responseString JSONValue];

注意:在服务器上执行时,查询工作正常。

2 个答案:

答案 0 :(得分:1)

在到达iPhone之前,在每个点进行故障排除。如果在数据库主机上运行查询,您会得到什么?如果您通过Web服务器运行查询,您会得到什么JSON响应?

另外,您使用什么框架来解析JSON?粘贴在一个代码片段中,显示您如何解析响应可能有助于提供更有针对性的答案。

答案 1 :(得分:1)

将我的查询更改为

SELECT a.FundID, a.FundName, a.Strike, a.LongShort, a.Current, a.Points, a.OpenClose
FROM tbl_Positions a, tbl_FundStatic b
WHERE b.FundID = a.FundID
AND b.UserID = '14'
AND a.OpenClose != 'Close'
UNION 
SELECT c.FundID, c.FundName, 0 AS Strike, "-" AS LongShort, b.LastTradePrice, 0 AS Points, "-" AS OpenClose
FROM tbl_FundStatic c, tbl_MarketData b
WHERE c.UserID = '14'
AND b.IndexCode = c.`Index` 
AND c.FundID NOT 
IN 
(
      SELECT DISTINCT (FundID)
      FROM tbl_Positions
)

刚刚删除了小数值周围的单引号'