English 中文(简体)
借助地体将javascript的关联阵列变成了json的物体,反之亦然。
原标题:Convert a javascript associative array into json object using stringify and vice versa

我有一阵列,如以下一环。

 var my_cars= new Array()
 my_cars["cool"]="Mustang";
 my_cars["family"]="Station Wagon";
 my_cars["big"]="SUV";

I want to convert it using Stringify to json object. I want to know after conversion how the json object will look like.

Also when i have this object How I can convert it back to associative array again.

问题回答

First of all, by making my_cars an array and stringifying it, you don t get what you expect.

var my_cars= new Array()
my_cars["cool"]="Mustang";
my_cars["family"]="Station Wagon";
my_cars["big"]="SUV";
alert(JSON.stringify(my_cars));

该通知<代码>[。

您希望从<代码>{}开始:

var my_cars= {};
my_cars["cool"]="Mustang";
my_cars["family"]="Station Wagon";
my_cars["big"]="SUV";
alert(JSON.stringify(my_cars));

这一警告

{"cool":"Mustang","family":"Station Wagon","big":"SUV"}

将物体从舱面上回收,使用JSON.parse()。

var s = JSON.stringify(my_cars);
var c = JSON.parse(s);
alert(c.cool);

这一警告 "Mustang".

See http://jsfiddle.net/Y2De9/

把我的意见变成一个答案,以便我能向你们展示一个法典榜样。

这些阵列在 j中没有。 您应当把物体用于像这样的非数字钥匙。 射线指数应为数字。 Java印物体可对钥匙使用任意价值(如你的例子)。 由于Arrays自己是物体,“耳光”会发生,但你不会找到正常的Array方法。 例如,研究这一法典的例子。

var my_cars= new Array()
my_cars["cool"]="Mustang";
my_cars["family"]="Station Wagon";
my_cars["big"]="SUV";
alert(my_cars.length);  // alerts 0

You have only added properties to the underlying object, not actually added elements to the Array. You should use an Object for this, not an Array. Javascript does not actually have an Associative Array. It has an Object who s properties can often be used like one would use an Associate Array in other languages. But, it s an Object, not an Array.

"JavaScript does not support arrays with named indexes"

The most close state to an associative array is an array with entries converted to properties (as in your case), so I provide a solution for this exact case.

幸运的是, Chrome星公司认为它是一种关联阵列:["cool”:“Mustang”、“家庭”:“Station Wagon”、“big”:“SUV”](Check with )

NOTE: open browser s console before running the snippet

var my_cars= new Array()
my_cars["cool"]="Mustang";
my_cars["family"]="Station Wagon";
my_cars["big"]="SUV";


let toAssociative=(keys, values)=> 
    values.reduce((acc, cv)=>{
       acc[acc.shift()]=cv
       return acc;
    }, keys)    


let fromAssociative = (assArr)=>({...assArr})

let serialized = JSON.stringify(fromAssociative(my_cars))

let o = JSON.parse(serialized)

let restored = toAssociative(Object.keys(o) , Object.values(o))

//NOTE: Look at the browser s console before executing (not SO console)

console.log("orig:",my_cars) 
//[cool: "Mustang", family: "Station Wagon", big: "SUV"]

console.log("serialized:",serialized)
//{"cool":"Mustang","family":"Station Wagon","big":"SUV"}
 
console.log("restored:",restored) //NOTE: look at the browser s console (F12)
//[cool: "Mustang", family: "Station Wagon", big: "SUV"]

If for some reason you cannot convert your array into object, for instance you are working on a big framework or legacy code that you dont want to touch and your job is only to add som feature which requires JSON API use, you should consider using JSON.stringify(json,function(k,v){}) version of the API. In the function you can now decide what to do with value of key is of a specific type.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签