Javascript 访问名称/值对

发布于 2024-08-15 01:52:25 字数 326 浏览 4 评论 0原文

我得到的 JSON 名称/值对如下所示:

{
   "Name":"parentid",
   "Value":"blah"
},
{
   "Name":"siteid",
   "Value":"blah"
},
{
   "Name":"sitename",
   "Value":"blah"
}

但我想访问“名称”值作为 KEY,将“值”值作为 VALUE 访问。有没有一种优雅的方法可以将那段 JSON 变成这样的东西?

{'parentid', 'blah'},
{'sitename', 'blah'}

I'm getting JSON name/value pairs that looks like this:

{
   "Name":"parentid",
   "Value":"blah"
},
{
   "Name":"siteid",
   "Value":"blah"
},
{
   "Name":"sitename",
   "Value":"blah"
}

But I would like to access the "name" value as the KEY, and the "value" value as the VALUE. Is there an elegant way to turn that piece of JSON into something like this?

{'parentid', 'blah'},
{'sitename', 'blah'}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风尘浪孓 2024-08-22 01:52:25

试试这个:

var items = [
    {
       "Name":"parentid",
       "Value":"blah"
    },
    {
       "Name":"siteid",
       "Value":"blah"
    },
    {
       "Name":"sitename",
       "Value":"blah"
    }
];

var results = new Object();

for (var i = 0; i < items.length; i++)
{
    results[items[i].Name] = items[i].Value;
}

这将导致类似的结果:

var results = { parentid: "Blah", siteid: "Blah", sitename: "Blah" };

Try this:

var items = [
    {
       "Name":"parentid",
       "Value":"blah"
    },
    {
       "Name":"siteid",
       "Value":"blah"
    },
    {
       "Name":"sitename",
       "Value":"blah"
    }
];

var results = new Object();

for (var i = 0; i < items.length; i++)
{
    results[items[i].Name] = items[i].Value;
}

This will result in something like:

var results = { parentid: "Blah", siteid: "Blah", sitename: "Blah" };
爱情眠于流年 2024-08-22 01:52:25

一种方法可以做到这一点。

var json = [
  {
    "Name":"parentid",
    "Value":"blah"
  },
  {
    "Name":"siteid",
    "Value":"blah"
  },
  {
    "Name":"sitename",
    "Value":"blah"
  }
];

for ( var i = 0, l = json.length, obj; i < l; i++ )
{
  obj = json[i];
  json[i] = new Object();
  json[i][obj.Name] = obj.Value;
} 

// console.log() requires Firebug
console.log( json );

One way to do it.

var json = [
  {
    "Name":"parentid",
    "Value":"blah"
  },
  {
    "Name":"siteid",
    "Value":"blah"
  },
  {
    "Name":"sitename",
    "Value":"blah"
  }
];

for ( var i = 0, l = json.length, obj; i < l; i++ )
{
  obj = json[i];
  json[i] = new Object();
  json[i][obj.Name] = obj.Value;
} 

// console.log() requires Firebug
console.log( json );
番薯 2024-08-22 01:52:25
function objectflatten (array) {
  var out = {}, i;
  for(i = 0; i < array.length; i++) {
     out[array[i].name] = array[i].value;
  }
  return out;
}

这是一个函数,它将采用您所呈现的形式的对象,并将其输出为“普通”对象,其中名称值作为键,值作为值。

function objectflatten (array) {
  var out = {}, i;
  for(i = 0; i < array.length; i++) {
     out[array[i].name] = array[i].value;
  }
  return out;
}

This is a function that will take an object in the form you presented, and output it as a "normal" object with the name values as keys, and the value values as values.

一笔一画续写前缘 2024-08-22 01:52:25

我建议使用 for( ... in ... ) 方法来完成此任务。它会根据您的需要获取关键名称。

var jsonObj = eval( '([{ "Name":"parentid", "Value":"blah" }])' );

for( var i = 0, assoc = {}, key; i < jsonObj.length; ++i )
{
    for( key in jsonObj[ i ] ) // <-- this right here
    {
        assoc[ key ] = jsonObj[ i ][ key ];
    }
}

你最终得到(来自 Firebug)

Object Name=parentid Value=blah

,可以通过 object.key 或 object[ 'key' ] 访问(在我们的例子中是 assoc.Name 或 assoc[ 'Value '])

这是来自雅虎的道格拉斯·克罗克福德(Douglas Crockford)的链接!关于使用它 - http://yuiblog.com/blog /2006/09/26/为了阴谋/

I'd recommend using the for( ... in ... ) method for this task. It'll grab the key names like you need.

var jsonObj = eval( '([{ "Name":"parentid", "Value":"blah" }])' );

for( var i = 0, assoc = {}, key; i < jsonObj.length; ++i )
{
    for( key in jsonObj[ i ] ) // <-- this right here
    {
        assoc[ key ] = jsonObj[ i ][ key ];
    }
}

and you end up with (from Firebug)

Object Name=parentid Value=blah

that can be accessed by object.key or object[ 'key' ] (in our case assoc.Name or assoc[ 'Value' ])

here's a link from Douglas Crockford from Yahoo! about using it as well - http://yuiblog.com/blog/2006/09/26/for-in-intrigue/

淡看悲欢离合 2024-08-22 01:52:25

我假设你正在使用 PHP,并且 PHP 会像这样回应你的关联:

echo json_encode($result); 

在你的 javascript 中,你可以这样做:

// Soemthing retrieves php result and puts it in `var result`.
data = eval("(" + result+ ")");
alert(data.parentid);

我不确定这是否是你想要的,但它是一个解决方案。

I'm assuming you are using PHP, and the PHP echoes you assosiatice aray like this:

echo json_encode($result); 

In your javascript, you could do this:

// Soemthing retrieves php result and puts it in `var result`.
data = eval("(" + result+ ")");
alert(data.parentid);

I'm not sure if this is what you want, but it's a solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文