[英]Get and Set Text Content from a JSON Object using JavaScript in node.js
Background information: I'm using node.js and this is a server side script. 背景信息:我正在使用node.js,这是服务器端脚本。
That being said, here's the problem. 话虽这么说,这就是问题所在。 I have a JSON Object
result
, when using eyes.inspect()
, looks like 我在使用
eyes.inspect()
时有一个JSON对象result
,看起来像
{
user: [
{
foods: {
food: [
{
#: 'McDonalds',
@: { type: 'string' }
},
{
#: 'Seafood Topped Salmon',
@: { type: 'string' }
}
]
},
email: '****@******.edu',
name: 'Leo'
},
{
email: '****@******.edu',
food-list: {
food: [
{
#: 'KFC',
@: { type: 'string' }
},
{
#: 'KGC',
@: { type: 'string' }
}
]
},
name: 'Eric'
}
]
}
When calling console.log(result.user[0].foods.food[1])
, the output is { '#': 'Seafood Topped Salmon', '@': { type: 'string' } }
调用
console.log(result.user[0].foods.food[1])
,输出为{ '#': 'Seafood Topped Salmon', '@': { type: 'string' } }
So is there a way to get and set the text content of a node just like Seafood Topped Salmon
and get rid of the type attribute? 那么,是否有一种方法可以获取和设置节点的文本内容,就像
Seafood Topped Salmon
并且摆脱了type属性?
Some more information: that JSON is actually parsed from an XML document. 一些更多信息:JSON实际上是从XML文档中解析出来的。 In the original xml file, the food node looks like
<food type="string">McDonalds</food>
. 在原始xml文件中,food节点类似于
<food type="string">McDonalds</food>
。 I must keep the type="string"
attribute and after I'm done with editing the JSON object, I will parse it back to xml. 我必须保留
type="string"
属性,并在完成JSON对象的编辑后,将其解析回xml。
您可以直接访问该嵌入式对象的文本来获取并将其设置为:
result.user[0].foods.food[1]['#']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.