为什么Android会错误地解释此JSON结果

时间:2016-10-04 05:55:35

标签: c# android jquery json android-webview

我有一个.NET页面,在传递参数时会返回JSON个对象。

@using System.Diagnostics;
@using System.Text;

@{

    string fileName = new FileInfo(this.Request.Url.LocalPath).Name;
    const string logDateFormat = "yyyy-MM-dd HH:mm:ss.fff";

    // All response headers added to resolve error message in firefox
    Response.Charset = Encoding.UTF8.EncodingName;
    Response.ContentEncoding = Encoding.UTF8;
    Response.ContentType = "application/json";
    Response.Headers.Add("Content-Language", "en");

    // no layout - we return raw Json or nothing
    Layout = null;

    // this converts all the keys to lowercase.  fixes case sensitiviy issue
    IDictionary<string, string> queryParam = Request.QueryString.AllKeys.ToDictionary(k => k.ToLowerInvariant(), k => Request.QueryString[k]);

    int MapId;
    string MapIdKey = "mapid";
    Debug.WriteLine(DateTime.Now.ToString(logDateFormat) + " - " + fileName + " - Parsing MapId");
    if(queryParam.ContainsKey(MapIdKey))
    {

        string paramValue;
        queryParam.TryGetValue(MapIdKey, out paramValue);
        MapId = paramValue.AsInt(0);  //default zero if invalid
    }
    else
    {
        MapId = 0;
    }

    IEnumerable<dynamic> data = null;
    string sql = "EXEC sp_map_get_location_units @0";
    Database conn = null;
    if (MapId > 0)
    {
        try
        {
            Debug.WriteLine(DateTime.Now.ToString(logDateFormat) + " - " + fileName + " - Using Connection");
            using (conn = Database.Open("CtOnline_Custom"))
            {
                Debug.WriteLine(DateTime.Now.ToString(logDateFormat) + " - " + fileName + " - Execute Query - " + sql);
                data = conn.Query(sql, MapId);
            }

            Debug.WriteLine(DateTime.Now.ToString(logDateFormat) + " - " + fileName + " - Write Results to browser");
            var jsonData = Json.Encode(data);
            Response.Write(jsonData);
            //Json.Write(data, Response.Output);
            Debug.WriteLine(DateTime.Now.ToString(logDateFormat) + " - " + fileName + " - Finished write to browser");
        }
        catch (Exception ex)
        {
            Debug.Print(ex.GetType().ToString());
            Debug.Print(ex.Message);
            Debug.Print(ex.StackTrace);
            Json.Write("{}", Response.Output);
        }
        finally
        {
            if (conn != null)
            {
                conn.Close();
            }
        }
    }
    else
    {
        Debug.WriteLine(DateTime.Now.ToString(logDateFormat) + " - " + fileName + " - MapId is 0.  Stopping.");
    }
}

viewed in my browser看起来像完全格式化的JSON

[{
    "NodeId": 251717,
    "UnitName": "777 D #1",
    "UnitDesc": "CLB29",
    "Latitude": -34.579685211181641,
    "Longitude": 150.81907653808594,
    "AssembledTime": "\/Date(1475532051000)\/",
    "Heading": 164,
    "Speed": 0
}, {
    "NodeId": 252072,
    "UnitName": "CLB37",
    "UnitDesc": "CLB37",
    "Latitude": -34.423614501953125,
    "Longitude": 150.89083862304688,
    "AssembledTime": "\/Date(1473985709000)\/",
    "Heading": 0,
    "Speed": 0
}, {
    "NodeId": 252073,
    "UnitName": "CLB35",
    "UnitDesc": "CLB35",
    "Latitude": -34.4235954284668,
    "Longitude": 150.89083862304688,
    "AssembledTime": "\/Date(1473985751000)\/",
    "Heading": 0,
    "Speed": 0
}, {
    "NodeId": 252074,
    "UnitName": "CLB36",
    "UnitDesc": "CLB36",
    "Latitude": -29.820035934448242,
    "Longitude": 30.833461761474609,
    "AssembledTime": "\/Date(1473985801000)\/",
    "Heading": 0,
    "Speed": 0
}]

从Android的默认浏览器查看时,它显示为中文。亲自尝试:http://www.ctrackonline.com.au/ClearyBros/JSON/GetUnitsForLocation?mapid=1

这意味着它的调用页面无法正确使用它:

var ajaxOptions = {
    method: "GET",
    url: "/ClearyBros/JSON/GetUnitsForLocation",
    processData: true, //means data sent as querystring
    dataType: "json",
    data: { mapId: mapId },
    timeout: 10 * 1000,
    cache: false,
    headers: { "Accept": "application/json" }
}

$.ajax(ajaxOptions)
.done(function(results) {
  etc etc

我在3款不同的Android平板电脑上试过这个。 2在办公室和另一个在JB Hifi。我还安装并测试了多个浏览器。它失败了

  • 默认
  • 内置于自助服务终端应用“KB Remote”

适用于Firefox。不幸的是,我不能在自助服务终端应用中使用它。

我看不出如何解决这个问题。

0 个答案:

没有答案