我有一些数据:
{
"_id": ObjectId("528ae48e31bac2f78431d0ca"),
"altitude": "110",
"description": [
{
"id": "2",
"des": "test"
}
],
"id": "1",
"latitude": "24.9528802429251",
"longitude": "121.509316013427",
"name": "H hotel"
}
我希望得到描述长度并显示它的上下文
请帮助我!!
答案 0 :(得分:0)
这是一个JSON数据,您可以对该数据进行字符串解析。
var data = JSON.stringify({
"_id": "ObjectId(528ae48e31bac2f78431d0ca)",
"altitude": "110",
"description": [
{
"id": "2",
"des": "test"
}
],
"id": "1",
"latitude": "24.9528802429251",
"longitude": "121.509316013427",
"name": "H hotel"
})
var parseData = JSON.parse(data)
parseData.description.length
1
parseData.description[0].id
"2"
parseData.description[0].des
"test"
建议:您可以在此格式之外获取ObjectId(528ae48e31bac2f78431d0ca)的值,并将其作为String传递给_id值
答案 1 :(得分:0)
首先,你需要验证你的json,
{
"_id": "528ae48e31bac2f78431d0ca",
"altitude": "110",
"description": [
{
"id": "2",
"des": "test"
}
],
"id": "1",
"latitude": "24.9528802429251",
"longitude": "121.509316013427",
"name": "H hotel"
}
这是一个有效的json。现在你可以像这样做 -
$.getJSON('assets/json/demo.json', function(data) {
$.each(data, function(key, value){
if(key === 'description'){
var description_length = value.length;
}
});
});
要显示其内容,您可以再次循环“描述”一些类似的内容 -
$.each(value, function(index, val){
alert(index+'-'+val);
});
答案 2 :(得分:0)
您没有提供太多信息,但这里是一个概述。我假设你有一个JSON 字符串而不是一个对象。我会称他为“Jason Data”......或者更确切地说是jsonData
。
这样的东西用于说明目的(只有没有新的行,因为你不能以这种方式在JavaScript中执行多行字符串,但你明白了):
// Jason Data, head full of doubt, road full of promises
var jsonData = '{
"_id": "528ae48e31bac2f78431d0ca",
"altitude": "110",
"description": [
{
"id": "2",
"des": "test"
}
],
"id": "1",
"latitude": "24.9528802429251",
"longitude": "121.509316013427",
"name": "H hotel"
}';
然后治疗我们的门诊病人Jason Data:
// let's get his Mona Lisa
var data = JSON.parse(jsonData);
// since he holds numerous memories, let's first get the count (as you wish)
var numDescriptions = data.description.length;
// as if we were psychiatrists, we will now analyze each of his experiences
for ( var i = 0; i < numDescriptions; i++ ) {
// and write a poem about his life. (even though this goes against doctor-patient privilege, sue me)
console.log(data.description[i].id,data.description[i].des);
}
答案 3 :(得分:0)
你的问题不明确,无论如何,如果你想获得与关键“描述”相对应的价值,那么试试这个yourdata.description来获得它的价值
我已在http://jsfiddle.net/twmSk/1/
创建了这些步骤//storing your data into variable yourdata
var yourdata = {
"_id": ObjectId("528ae48e31bac2f78431d0ca"),
"altitude": "110",
"description": [
{
"id": "2",
"des": "test"
}
],
"id": "1",
"latitude": "24.9528802429251",
"longitude": "121.509316013427",
"name": "H hotel"
}
$('#test').html(JSON.stringify(yourdata.description))