-6

{ "name": "Chris", "age": "RIP", "musketeers": ["Athos", "Aramis", "Porthos", "D'Artagnan"], "stooges": [ { "name": "Moe" }, { "name": "Larry" }, { "name": "Curly" } ], "details": { "first": "Michael", "last": "Jackson" } }

Output should be:
name :Chris

age :RIP

musketeers: Athos,Aramis,Porthos,D'Artagnan

stooges: name:Moe name:Larry name:Curly

details: first :Michael last:Jackson

  • how to split elements in javascript? – Vikash Kumar Jul 11 '16 at 12:27
  • 2
    Please refer [How to ask](http://stackoverflow.com/help/how-to-ask) and provide necessary details – Rajesh Jul 11 '16 at 12:30
  • 2
    What exactly is the question? What do you want to split in this data? What have you tried till now? – Krishnakumar_Muraleedharan Jul 11 '16 at 12:31
  • i want to get keys and values of this json – Vikash Kumar Jul 11 '16 at 12:43
  • output should be :--- name :Chris age :RIP musketeers Athos,Aramis,Porthos,D'Artagnan stooges name:Moe name:Larry name:Curly details first :Michael last:Jackson – Vikash Kumar Jul 11 '16 at 12:48
  • 1
    Objects has `for..in` loop which gives you `keys` and `object[key]` will give you value. Also if you just wish to access value of a property, you could have found an appropriate answer easily. – Rajesh Jul 11 '16 at 12:49
  • Refer: http://stackoverflow.com/questions/4255472/javascript-object-access-variable-property-by-name-as-string and http://stackoverflow.com/questions/8312459/iterate-through-object-properties – Rajesh Jul 11 '16 at 12:50

3 Answers3

0

JSON is a valid JS object. you can get name:var name=data.name; and musketeers

var musketeers = data.musketeers;
for (var i = 0; i < musketeers.length; i++) {
  console.log(musketeers[i]);
}
Rajesh
  • 24,354
  • 5
  • 48
  • 79
  • output should be :- name :Chris age :RIP musketeers Athos,Aramis,Porthos,D'Artagnan stooges name:Moe name:Larry name:Curly details first :Michael last:Jackson – Vikash Kumar Jul 11 '16 at 12:44
  • 1
    @VikashKumar Instead of spreading vague ideas of what you actually want throughout various comment sections, how about [editing](http://stackoverflow.com/posts/38306894/edit) your question and providing all necessary details there? – Sebastian Simon Jul 11 '16 at 12:48
0
<html>
<head>
<script>
window.onload = function() {
var jsonValue={
                "name": "Chris",
                "age": "RIP",
                "musketeers": ["Athos", "Aramis", "Porthos", "Artagnan"],
                "stooges": [
                            { "name": "Moe" },
                            { "name": "Larry" },
                            { "name": "Curly" }
                            ],
                "name details": {
                "first": "Michael",
                "last": "Jackson"
                }
            };
if (!Array.prototype.inArray) {
    Array.prototype.inArray = function(element) {
        return this.indexOf(element) > -1;
    };
}                           
var key,key1,key2,innerdiv = '';
var array = ['0','1','2','3','4','5','6','7','8','9','10','11'];
//console.log(jsonValue);
for(key in jsonValue){
    if(jsonValue.hasOwnProperty(key)) {
        if(typeof(jsonValue[key])=='object'){
            //console.log(key);
            innerdiv+="<div>"+key;
            for(key1 in jsonValue[key]){
                if(jsonValue[key].hasOwnProperty(key1)){
                    //console.log();
                    // console.log(key1 + " = " + jsonValue[key][key1]);
                    //console.log(typeof(key1));
                    innerdiv+= "<p>";
                    if(typeof(jsonValue[key][key1]) =='object'){
                        for(key2 in jsonValue[key][key1]){
                            innerdiv+= key2 + " <input type='text' name='"+key2+"' value='"+jsonValue[key][key1][key2]+"'></p>";
                        }
                    }
                    else{

                        if(array.inArray(key1)){
                            innerdiv+= " <input type='text' name='"+key1+"' value='"+jsonValue[key][key1]+"'></p>";
                        }
                        else{
                            innerdiv+= key1 + " <input type='text' name='"+key1+"' value='"+jsonValue[key][key1]+"'></p>";
                        }
                        //console.log('test'+key1+'test');

                    }
                      //console.log("6"+typeof(jsonValue[key][key1]));
                }
            }
            innerdiv+="</div>";
        }
        else{
            innerdiv+= "<p>"+key+" <input type='text' name='"+key+"' value='"+jsonValue[key]+"'></p>";
            //console.log(key + " = " + jsonValue[key]);
        }
    }

} 
//console.log(innerdiv );
document.getElementById("htmlContent").innerHTML = innerdiv;
};

</script>
</head>

<form name="" action="" method="">
        <div id="htmlContent"></div>
        <input type="submit" name="submit" value="submit">
</form>
</html>
-1

Try this

use the function JSON.Parse();

Jishnu KM
  • 234
  • 1
  • 8
  • i used but did not get elements – Vikash Kumar Jul 11 '16 at 12:29
  • 3
    @VikashKumar your question is missing basic details. Unless you provide them, people will give you answer but based on their interpretation, which may be wrong. So I suggest, you provide these details like, what are you trying to achieve, what have you tried etc. If you can create a JSFiddle, that would be even better. – Rajesh Jul 11 '16 at 12:37
  • output should be name :Chris age :RIP musketeers Athos,Aramis,Porthos,D'Artagnan stooges name:Moe name:Larry name:Curly details first :Michael last:Jackson – Vikash Kumar Jul 11 '16 at 12:48