json_decode是圆形浮动,我该如何防止它?

die*_*vdf 9 php json decode

我有一个相当大的json文件,其坐标格式如下

"[[3.2,1],[4.8,2]]"
Run Code Online (Sandbox Code Playgroud)

代表(3.2,1)和(4.8,2)

我正在使用这些coördinates生成D3地理地图,但是当php将此信息建模为geoJSONobject时,我遇到以下错误:

我需要将坐标转换为我使用的数组json_decode.然而:

json_decode("[[3.2,1],[4.8,2]]")
Run Code Online (Sandbox Code Playgroud)

回报

Array
(
[0] => Array
    (
        [0] => 3
        [1] => 1
    )
[1] => Array
    (
        [0] => 4
        [1] => 2
    )
)
Run Code Online (Sandbox Code Playgroud)

我丢失小数的地方.我怎么能阻止这个?

编辑:

{"type": "FeatureCollection",
 "features": [{
        "type": "Feature",
        "geometry": {
            "type": "Polygon",
            "coordinates": "[[[8.7, 11], [8.89, 12.13],[9.27, 12.13], [9.9, 12], [9.7, 10.8], [8.7, 11]]]"
        },
        "properties": {
            "name": "04",
            "count": "25"
        }
    }]
}
Run Code Online (Sandbox Code Playgroud)

这是我作为输出得到的数据的一个例子.(它应该代表一个房间地图,根据其用途获得密度颜色)

我可以使用解析它jQuery.parseJSON(data),但运行以下D3代码会产生最奇怪的错误:

val(svgname).append("g")
    .selectAll("path")
    .data(geoJSONobject.features)
    .enter().append("path")
    .attr("d", path)
    ...
Run Code Online (Sandbox Code Playgroud)

错误 我认为这是因为坐标数组周围的引号.

编辑(2) - 实际解决方案

我接受的解决方案是一种解决方法,但真正的问题是本地化的php-settings.使用:

echo json_encode($dataset, JSON_NUMERIC_CHECK);
Run Code Online (Sandbox Code Playgroud)

在php文件中,所有问题都得到了解决.虽然我会更新这个问题,因为它仍在被查看(如果有人会解决这个问题)

Abh*_*nav 8

我有同样的问题.我使用followin regex解决了它

解决方案1

$yourJsonVariable = preg_replace('/:\s*(\-?\d+(\.\d+)?([e|E][\-|\+]\d+)?)/', ': "$1"', $yourJsonVariable);
Run Code Online (Sandbox Code Playgroud)

将其转换为数组

$array = json_decode($yourJsonVariable, true);
Run Code Online (Sandbox Code Playgroud)

积分归于此SO LINK

解决方案2

你可以设置 ini_set('precision',1);

解决方案3

$decoded = json_decode($encoded, true, null, JSON_BIGINT_AS_STRING);
Run Code Online (Sandbox Code Playgroud)

注意: Last解决方案仅适用于PHP> 5.4

您可能想看一下这个博客


Tra*_*ty3 0

只需将值括在引号中即可:json_decode('[["3.2","1"],["4.8","2"]]');