繁体   English   中英

更新javascript中的JSON.parsed对象

[英]Update JSON.parsed object in javascript

我正在尝试更新解析的对象。数据是从服务器收到的JSON对象。

var document_text = JSON.parse(data.projectData[0]['document_text']);
for(var j = 0; j < document_text.length;j++)
{
    var k = JSON.parse(document_text[j]);
    var document_data = JSON.parse(k).text;
    var page_number = JSON.parse(k).page_number;
}

现在我要更新包含文本和page_number字段的document_text对象。请注意,我必须对该对象进行两次解析。首先解析外部值,然后获取内部值。如何更新document_text的字段(即text,page_number)。

这是原始数据

 ["\"{\\\"text\\\":\\\"Osddsdsdsdsds \\\\n\\\\n to  as \\\\\\\"sdfdsdsfsdfsdfsdf\\\\\\\") and CPGsddsdsdsdssdROsdsdsdsdP sdsdds,  a \\\\sd  sdds\\\\n\\\\n\\\\f\\\",\\\"page_number\\\":44}\"","\"{\\\"text\\\":\\\"Page  14 \\\\n\\\\nsdfsdfsdfdscopysdsdds\\\n\\\\n\\\\f\\\",\\\"page_number\\\":45}]
var document_text = JSON.parse(data.projectData[0]['document_text']);

/* At this point, document_text is already a JSON object. Iterating over it with 
a for loop doesn't make much sense. You can now just access its properties directly. */

document_text.text = "Some other text";
document_text.page_numer = 1;

/* Now we can return it to where it came from by stringify'ing it */

data.projectData[0]['document_text'] = JSON.stringify(document_text);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM