Farzaneh Talebi
Farzaneh Talebi

Reputation: 915

how to get key and value from string

Here is a string like this:

var data =  {ListofLayers: "[{"Key":"G1","Value":"park"}]"}

I use below code for get key and value:

var JSONObject = JSON.parse(JSON.stringify(data));
var obj = JSON.parse(Object.values(JSONObject));

$.each(obj, function (key, value) {
     $.each(value, function (key, value) {
        console.log(key + ':' + value);
     });
});

but it return

Key:G1
Value:park

but I expected G1:park.

what is wrong?

 var data =  {ListofLayers: '[{"Key":"G1","Value":"park"}]'};


    var JSONObject = JSON.parse(JSON.stringify(data));
    var obj = JSON.parse(Object.values(JSONObject));
    
    $.each(obj, function (key, value) {
         $.each(value, function (key, value) {
            console.log(key + ':' + value);
         });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

Upvotes: 0

Views: 1320

Answers (6)

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

You can use Object.values to get the values of the keys for the JSON object inside the ListofLayers array. Then, this will return an array which you can join using colon (:) like this,

var data = {ListofLayers:[{"Key":"G1","Value":"park"}]}
$(document).ready(function(){
  $.each(data.ListofLayers, function (key, value) {
      console.log(Object.values(value).join(':'));
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 0

Vignesh Raja
Vignesh Raja

Reputation: 8751

As Rajesh suggested in Dhruvin's answer, the following will work.

var data =  {ListofLayers: '[{"Key":"G1","Value":"park"}]'}
var result = Object.values(JSON.parse(data.ListofLayers)[0]).join(":")

console.log(result);

Upvotes: 0

sumit chauhan
sumit chauhan

Reputation: 1300

you can do something like below with javascript ES6.

var data =  {ListofLayers: [{"Key":"G1","Value":"park"}]};

var JSONObject = JSON.parse(JSON.stringify(data));
var result ={};
for(let keys of JSONObject.ListofLayers) {
  result = {[keys.Key]: keys.Value};
}
console.log(result);

Upvotes: 0

Eddie
Eddie

Reputation: 26844

Ypu dont really need the second each. You can access the json property as value["Key"] or value.Key

var JSONObject = {
  ListofLayers: '[{"Key":"G1","Value":"park"}]'
};
var obj = JSON.parse(Object.values(JSONObject));

$.each(obj, function(key, value) {
  console.log(value["Key"] + ":" + value["Value"]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Upvotes: 0

NnN
NnN

Reputation: 463

Check this - https://jsfiddle.net/6rLsw56n/

var data =  {ListofLayers: '[{"Key":"G1","Value":"park"}]'};
var JSONObject = JSON.parse(JSON.stringify(data));
var obj = JSON.parse(Object.values(JSONObject));

$.each(obj, function (key, value) {
    var s = '';
    $.each(value, function (key, value) {
        s += (s == '' ? value : (':' + value));       
    });
    console.log(s);
});

Upvotes: 0

Dhruvin
Dhruvin

Reputation: 164

See Working Demo :

var data =  {ListofLayers:[{"Key":"G1","Value":"park"}]}


$(document).ready(function(){
$.each(data.ListofLayers, function (key, value) {
    console.log(value.Key+":"+value.Value);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 1

Related Questions