JSON.stringify不转换数组

JSON.stringify does not convert arrays

本文关键字:数组 转换 stringify JSON      更新时间:2023-10-15

我知道我做这件事的方式不对,老实说,这可能是因为我处理递归不正确,但如果是这样,我不确定哪里出了问题。这是示例的链接。

这是JavaScript-

function propertyTest(currentObject, key) {
    for (var property in currentObject) {
        if (typeof currentObject[property] === "object") {
            propertyTest(currentObject[property], property);
        } else {
            // this is only to test the output
            $('#method1').append((property == 'value' && key ? key : property) + ' -- ' + currentObject[property] + '<br />');
            propertyKey = (property == 'value' && key ? key : property);
            propertyValue = currentObject[property];
            var arrayJSON = [];
            arrayJSON.push(propertyKey);
            arrayJSON.push(propertyValue);
        }
    }
    var JSONString = JSON.stringify(arrayJSON);
    console.log(JSONString);
    return JSONString;
}

这是原始的JSON-

var oarsObject = [{
    "coordinateReferenceSystem": "26782,15851",
    "positionReferenceType": "geogWgs84",
    "geogWgs84": {
        "latitude": {
            "value": 0.50507158458214041
        },
        "longitude": {
            "value": -1.604064725846865
        },
        "height": {
            "value": 0.0
        }
    }
}, {
    "coordinateReferenceSystem": "26782,15851",
    "positionReferenceType": "geogWgs84",
    "geogWgs84": {
        "latitude": {
            "value": 0.50509265195620767
        },
        "longitude": {
            "value": -1.5961047836759397
        },
        "height": {
            "value": 0.0
        }
    }
}, {
    "coordinateReferenceSystem": "26782,15851",
    "positionReferenceType": "geogWgs84",
    "geogWgs84": {
        "latitude": {
            "value": 0.4963464715653228
        },
        "longitude": {
            "value": -1.5960947041991222
        },
        "height": {
            "value": 0.0
        }
    }
}, {
    "coordinateReferenceSystem": "26782,15851",
    "positionReferenceType": "geogWgs84",
    "geogWgs84": {
        "latitude": {
            "value": 0.49632551101280209
        },
        "longitude": {
            "value": -1.604015267530267
        },
        "height": {
            "value": 0.0
        }
    }
}]

JSON被发送到您所期望的函数-

propertyTest(oarsObject);

以下是它偏离轨道的地方的证据,来自控制台的一个片段-

["latitude",0.5050715845821404]
["longitude",-1.604064725846865]
["height",0]
undefined
["positionReferenceType","geogWgs84"]
["latitude",0.5050926519562077]
["longitude",-1.5961047836759397]
["height",0]
undefined

请注意前两个项目最初是如何出现在日志中的,之后仅出现在positionReferenceType中。还要注意未定义的JSON字符串。我确信这是因为我的递归是错误的。

在阅读了其他几篇文章后,我知道JavaScript数组想要数字键,但我很好奇。JSON.stringify()似乎对其中的一些问题有效,而对其他问题无效。此外,在第一轮positionReferenceType被字符串化后,结果是不一致的,尽管顺序显然是错误的(同样,我确信这是因为我的递归工作失败了)。

这个用例有两个方面。首先,我们想去掉原始JSON中不必要的"value"键,该键是由系统的一部分生成的,此时我们无法修改。其次,系统的其他部分会消耗我们希望从这样的函数输出的离散较小的JSON位。输出应该是小的、单独的JSON字符串(类似于HTML输出中显示的内容)输出应该是单个JSON字符串,由单个键/值对组成,如下例所示

[
    {
        "coordinateReferenceSystem": "26782,15851"
    },
    {
        "positionReferenceType": "geogWgs84"
    },
    {
        "latitude": 0.5050715845821404
    },
    {
        "longitude": -1.604064725846865
    },
    {
        "height": 0
    }
]

我甚至还没有接近组装整个JSON字符串的地步,因为我只是试图正确地取出对。

我确信我在这里俯瞰着什么。我应该创建一个对象,而不是一个数组来将所有内容字符串化吗?或者是递归中的某些东西绊倒了我,而不是我认为我提到的明显问题?

通常有两种方法可以递归并得到一个平坦的数组。

  • 首先是将结果数组作为参数传递,每次重复都会添加到其中
  • 第二,每个调用都返回一个数组,上级将其添加到自己的结果中

无论哪种方式,您都需要先测试Array主题,然后检查Object主题,因为Array是Object,通常您会想为它们做不同的事情。

此外,通常情况下,您将在递归之外处理结果(例如,转换为JSON,然后记录),以保持递归简短。

这里是使用第一种方法的实现,其中包括顶级的JSON转换。我已尽可能重复使用您的变量名。

function propertyTest( currentObject, array, key ) {
   var result = array || [], o;
   if ( Array.isArray( currentObject ) ) {
      currentObject.forEach( function ( e ) { propertyTest( e, result ); } );
   } else if ( typeof ( currentObject ) === 'object' ) {
      if ( 'value' in currentObject && Object.keys( currentObject ).length === 1 ) {
         propertyTest( currentObject.value, result, key );
      } else {
         for ( var property in currentObject ) {
            propertyTest( currentObject[ property ], result, property );
         }
      }
   } else {
      result.push( o = {} );
      o[ key ] = currentObject;
   }
   return array === undefined ? JSON.stringify( result ) : result;
}
var oarsObject = [{
    "coordinateReferenceSystem": "26782,15851",
    "positionReferenceType": "geogWgs84",
    "geogWgs84": {
        "latitude": {
            "value": 0.50507158458214041
        },
        "longitude": {
            "value": -1.604064725846865
        },
        "height": {
            "value": 0.0
        }
    }
}, {
    "coordinateReferenceSystem": "26782,15851",
    "positionReferenceType": "geogWgs84",
    "geogWgs84": {
        "latitude": {
            "value": 0.50509265195620767
        },
        "longitude": {
            "value": -1.5961047836759397
        },
        "height": {
            "value": 0.0
        }
    }
}, {
    "coordinateReferenceSystem": "26782,15851",
    "positionReferenceType": "geogWgs84",
    "geogWgs84": {
        "latitude": {
            "value": 0.4963464715653228
        },
        "longitude": {
            "value": -1.5960947041991222
        },
        "height": {
            "value": 0.0
        }
    }
}, {
    "coordinateReferenceSystem": "26782,15851",
    "positionReferenceType": "geogWgs84",
    "geogWgs84": {
        "latitude": {
            "value": 0.49632551101280209
        },
        "longitude": {
            "value": -1.604015267530267
        },
        "height": {
            "value": 0.0
        }
    }
}];
alert( propertyTest( oarsObject ) );

正如Sheepy所说,您可能希望在递归函数之外确定结果数组的范围。

当它是数组值时使用forEach,当它是对象中的对象时使用for。通过这种方式,您只处理一个对象的推送,而不必维护数组。

示例jsFiddle

var arrayJSON = [];
function propertyTest(currentObject, key) {
    if (Array.isArray(currentObject)) {
        currentObject.forEach(propertyTest);
    } else {
        for (var returnKey in currentObject) {
            if (typeof currentObject[returnKey] === 'object') {
                propertyTest(currentObject[returnKey], returnKey);
            } else {
                var newKey = (returnKey === 'value' && key) ? key : returnKey;
                var newObj = {};
                newObj[newKey] = currentObject[returnKey];
                arrayJSON.push(newObj);
                $('#method1').append( newKey + ' -- ' + currentObject[returnKey] + '<br />');
            }
        }
    }
}
propertyTest(oarsObject);
console.log(JSON.stringify(arrayJSON););