在localstorage中,我有以下值的关键' 结果 ' :
[{"id":"item-1","href":"google.com","icon":"google.com"},
{"id":"item-2","href":"youtube.com","icon":"youtube.com"},
{"id":"item-3","href":"google.com","icon":"google.com"},
{"id":"item-4","href":"google.com","icon":"google.com"},
{"id":"item-5","href":"youtube.com","icon":"youtube.com"},
{"id":"item-6","href":"asos.com","icon":"asos.com"},
{"id":"item-7","href":"google.com","icon":"google.com"},
{"id":"item-8","href":"mcdonalds.com","icon":"mcdonalds.com"}]
Run Code Online (Sandbox Code Playgroud)
要获得最后一项,我使用此:
// this is how I parse the arrays
var result = JSON.parse(localStorage.getItem("result"));
for(var i=0;i<result.length;i++) {
var item = result[i];
$('element').val(item.href);
}
Run Code Online (Sandbox Code Playgroud)
如何获取item-3或特定ID的href?
我正在用json数据保存一个cookie.回声的例子$_COOKIE['data']
[{\ "日期\":1355249777,\ "标题\":\ "初级\"},{\ "日期\":1355249747,\ "标题\":\ "克里斯托弗\"},{\"日期\ ":1355249139,\" 标题\ ":\" 梅尔菲\ "},{\" 日期\ ":1355249123,\" 标题\ ":\" 托尼\ "},{\" 日期\ ":1355248876,\"标题\ ":\" 卡梅拉\ "},{\" 日期\ ":1355248859,\" 标题\ ":\" 草地\"}]
数据是纯javascript,然后传递JSON.stringify然后存储在cookie中.现在我需要将其转换为php数组.我尝试了一种json_decode方法,但它返回null.有任何想法吗?谢谢!
下面的代码应输出100我的字符串化知识.vstr(s)应该扩展为100,然后str(s)得到100,它应该返回字符串"100".但是,它输出"a"代替.是什么原因?但是,如果我用宏定义的常量调用,foo那么它输出"100".为什么?
#include<stdio.h>
#define vstr(s) str(s)
#define str(s) #s
#define foo 100
int main()
{
int a = 100;
puts(vstr(a));
puts(vstr(foo));
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在将一个对象转换为一个数组.对象的键包含诸如"cool"之类的名称,如下所示:
var obj = {
"cool": "Mustang",
"family": "Station Wagon",
"small": {
0: "small car 1",
1: "small car 2"
}
};
Run Code Online (Sandbox Code Playgroud)
转换时,返回的数组如下所示:
Array[3]
0:"Mustang"
1:"Station Wagon"
2:Object
0:"small car 1"
1:"small car 2"
Run Code Online (Sandbox Code Playgroud)
正如您在上面的数组中看到的那样,来自对象的键的名称(例如"cool")将丢失,而是将它们替换为数字.此外,数组包含一个对象,我希望这个对象是数组中的一个数组.
我希望返回的数组是这样的:
Array[3]
"cool":"Mustang"
"family":"Station Wagon"
"small": Array[2]
0:"small car 1"
1:"small car 2"
Run Code Online (Sandbox Code Playgroud)
我非常感谢你的帮助.可以在这里找到代码的小提琴:https://jsfiddle.net/v02q4sy2/8/
我正在尝试使用 Typescript 修改字符串。该字符串是由JSON.stringify()方法创建的。
我想删除属性"id"、"lightStatus"以及"inputPort"和"outputPort"的"value"属性。(我只需要他们的属性“id”。)
console.log(JSON.stringify(this.light));
// Results in -> {"id":1,"name":"Light Switch","lightStatus":true,"inputPort":{"id":2,"value":0},"outputPort":{"id":2,"value":false},"resistance":100}
Run Code Online (Sandbox Code Playgroud)
我尝试按照以下方式执行此操作,但它无法识别“inputPort.id”和“outputPort.id”。这就是我的尝试和结果。
var savedLight = JSON.stringify(this.light, ["name", "inputPort.id", "outputPort.id", "resistance"]);
// Results in -> {"name":"Light Switch","resistance":100}
Run Code Online (Sandbox Code Playgroud)
结果应包含属性"name"、"inputPort id"、"outputPort id" 和 "resistance"。像这样:
{"name":"Light Switch","inputPort": 2, "outputPort": 2, "resistance":100}
Run Code Online (Sandbox Code Playgroud)
谁能帮助我如何摆脱不必要的属性?
我很好奇像moment这样的库如何JSON.stringify在该对象上调用时自动从对象转换为字符串。
瞬间测试示例:https : //github.com/moment/moment/blob/3147fbc486209f0b479dc0b29672d4c2ef39cf43/src/test/moment/format.js#L144-L146
这是一些示例代码,我很好奇它是如何工作的
const moment = require('moment');
const duration = moment.duration(1374);
console.log('duration = ', duration); // Prints a duration object
console.log('JSON.stringify(duration) = ', JSON.stringify(duration)); // Prints a string such as `P0D1T0H3` <-- Note: Not exact value, just similar format
Run Code Online (Sandbox Code Playgroud) 我做了一项研究,奇怪的是我找不到检查对象是否被字符串化的正确方法。
我尝试像这样检查并分配它,但不起作用(它通过验证并只分配 arr_ ):
var arr = arr_ instanceof String ? JSON.parse(arr_) : arr_;
Run Code Online (Sandbox Code Playgroud) 对于以下代码:
const testStringify = () => {
try{
console.log('Inside testStringify');
const a = "";
a.b.c;
} catch(err) {
console.log('Inside catch of testStringify');
console.log(`Error: ${JSON.stringify(err)}`);
console.log(err);
}
}
testStringify();
Run Code Online (Sandbox Code Playgroud)
输出是:
Inside testStringify
Inside catch of testStringify
Error: {}
TypeError: Cannot read property 'c' of undefined
at testStringify
at Object.<anonymous>
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
Run Code Online (Sandbox Code Playgroud)
为什么 Json.stringify(err) (错误:{})不以与 console.log(err) 相同的方式或某种有意义的方式打印输出?
我有JSON stringify数据,如下所示:
[{"availability_id":"109465","date":"2017-02-21","price":"430000"},{"availability_id":"109466","date":"2017-02-22","price":"430000"},{"availability_id":"109467","date":"2017-02-23","price":"430000"}]
Run Code Online (Sandbox Code Playgroud)
我只想获得该数据的价格.我试过这种方式,但它不起作用.
var stringify = JSON.stringify(values);
for(var i = 0; i < stringify.length; i++)
{
alert(stringify[i]['price']);
}
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做?
我尝试在圆形结构后进行json分层和解析。我想在使用套接字io时将我的变量套接字传递给我的所有网站。在React中,这很容易,但是项目很旧,并且他们没有对所有项目都使用React。
我使用扁平化库,它对于字符串化非常有用,但是我没有得到我的原始元素。当我解析它时,我得到一个对象
const mySocket = io("monsite:1234");
const stringigy = JSON.stringify(mySocket);
localStorage.setItem("io",mySocket);
Run Code Online (Sandbox Code Playgroud)
不起作用,因为圆形结构。
带扁平
const mySocket = io("monsite:1234");
const stringigy = Flatted.stringify(mySocket);
localStorage.setItem("io",mySocket);
Run Code Online (Sandbox Code Playgroud)
工作,但是当我解析时,我没有得到原始值
socket = Flatted.parse(getIo);
Run Code Online (Sandbox Code Playgroud)
感谢帮助。