如何从字符串中获取键和值

时间:2018-02-13 07:17:22

标签: javascript jquery json

这是一个这样的字符串:

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

我在下面的代码中使用get key和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);
     });
});

但它返回

Key:G1
Value:park

但我期待G1:park

出了什么问题?

 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>

6 个答案:

答案 0 :(得分:1)

参见工作演示:

ViewHolder
private bool DatesAreInTheSameWeek(DateTime birthday)
{
    if (birthday == DateTime.MinValue)
    {
        return false;
    }
    else
    {
        var birtdayWithCurrentYear = new DateTime(DateTime.Today.Year, birthday.Month, birthday.Day);
        if (birthday.Month == 1 && DateTime.Today.Month != 1)
        {
            birtdayWithCurrentYear = birtdayWithCurrentYear.AddYears(1);
        }
        DateTime firstDayInWeek = DateTime.Today.Date;
        while (firstDayInWeek.DayOfWeek != CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek)
            firstDayInWeek = firstDayInWeek.AddDays(-1);var lastDayInWeek = firstDayInWeek.AddDays(7);

        return birtdayWithCurrentYear < lastDayInWeek && birtdayWithCurrentYear >= firstDayInWeek;

    }
}

答案 1 :(得分:0)

检查一下 - https://jsfiddle.net/6rLsw56n/

foo(r::Ref{Int}) = r[] *= 2

r=Ref{Int}(1)     # Base.RefValue{Int64}(1)
foo(r);
r                 # Base.RefValue{Int64}(2)

答案 2 :(得分:0)

Ypu真的不需要第二个dms =['Physical','VCR'] selectedNav: any; constructor( private dataService:DataService) {} ngOnInit() {} 。您可以访问json属性eachvalue["Key"]

&#13;
&#13;
value.Key
&#13;
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"]);
});
&#13;
&#13;
&#13;

答案 3 :(得分:0)

您可以使用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);

答案 4 :(得分:0)

正如Rajesh在Dhruvin的回答中所建议的那样,以下内容将起作用。

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

console.log(result);
&#13;
&#13;
&#13;

答案 5 :(得分:0)

您可以使用Object.values获取ListofLayers数组中JSON对象的键值。然后,这将返回一个数组,您可以使用冒号(:)这样加入

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>