-1

i have some data like :

{
    "_id": ObjectId("528ae48e31bac2f78431d0ca"),
    "altitude": "110",
    "description": [
        {
            "id": "2",
            "des": "test"
        }
    ],
    "id": "1",
    "latitude": "24.9528802429251",
    "longitude": "121.509316013427",
    "name": "H hotel"
}

i want get description length and show it context

please help me!!

Chandu
  • 81,493
  • 19
  • 133
  • 134
BruceYang
  • 9
  • 2

4 Answers4

0

This is a JSON data you can stringfy and parse that data.

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"

Suggestion: You can get the value of ObjectId(528ae48e31bac2f78431d0ca) outside this format, and pass that as String to _id value

  • *"This is a JSON data you can stringfy"* This statement is misleading. `JSON.stringify` is not applied to JSON but to arrays and objects, in order to **create JSON**. Furthermore, what's the point of creating JSON and parse it immediately? Just assign the object to a variable and work with it, e.g. `var parseData = {"_id": .... };`. – Felix Kling Nov 23 '13 at 06:53
  • @Felix Kling I understood my mistake, but I am expecting some more explaination – Suganthan Madhavan Pillai Nov 23 '13 at 06:57
  • Explanation for what? From whom? – Felix Kling Nov 23 '13 at 07:02
0

First of all you need to validate your json,

{
    "_id": "528ae48e31bac2f78431d0ca",
    "altitude": "110",
    "description": [
        {
            "id": "2",
            "des": "test"
        }
    ],
    "id": "1",
    "latitude": "24.9528802429251",
    "longitude": "121.509316013427",
    "name": "H hotel"
}

This is a valid json. Now you can do the desired like this -

$.getJSON('assets/json/demo.json', function(data) {
                $.each(data, function(key, value){
                    if(key === 'description'){
                        var description_length = value.length;

                    }
                });
            });

And to display its content you can again loop over "description" some what like this -

$.each(value, function(index, val){
   alert(index+'-'+val);
});
Anil Saini
  • 627
  • 3
  • 17
0

You don't provide much information, but here's an overview. I'm assuming you have a JSON string and not an object yet. I shall call him, "Jason Data" ...or rather jsonData.

Something like this for illustrative purposes (only without the new lines since you can't do multi-line strings in JavaScript in this fashion, but you get the idea):

// 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"
}';

And then treat our outpatient, 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);
}
zamnuts
  • 9,492
  • 3
  • 39
  • 46
0

Your question is not clear, anyway if you are looking to get value corresponding to key "description" then try this yourdata.description to get it's value

I have created these steps at 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))