从 JSON 检索引用数据

发布于 2024-12-02 01:40:06 字数 361 浏览 1 评论 0原文

我有以下格式的 json 数据

{"updates":
    {"message"   :"[[student:123]] is present."},
     "references":[{"type":"student","full_name":"XYZ","id":123}]
}

如何使用消息中存在的 id 将学生姓名映射到消息? 我对 JSON 解析比较陌生。我目前正在使用 EJS 模板将 JSON 操作为 HTML。

仅仅使用

<%alert(updates.message.student)%>

就会返回“未定义”。请帮忙。

I have json data in the following format

{"updates":
    {"message"   :"[[student:123]] is present."},
     "references":[{"type":"student","full_name":"XYZ","id":123}]
}

How can I map the student name to the message using the id present over the message?
I am relatively new to JSON parsing. I am currently using EJS template to manipulate the JSON into HTML.

In that just using

<%alert(updates.message.student)%>

returns "undefined". Please help.

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

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

发布评论

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

评论(2

梦里°也失望 2024-12-09 01:40:06

updates.message 是一个字符串,而不是 JavaScript 对象。您可以通过整个属性周围的引号来判断。 JavaScript 字符串没有 student 属性,因此您将得到 undefined。您可以使用正则表达式从字符串中解析出 JSON 部分,然后使用 JSON.parse() 获取 JSON 对象。但是,在您的示例中,学生 ID 也位于 updates.references[0].id 中。

要获取学生 ID,请执行以下操作:

<% alert(updates.references[0].id) %>

编辑:如果您确实想从消息中获取 ID,则需要以某种方式解析它。如果消息格式始终相同,您可以尝试使用正则表达式或字符串拆分来获取包含 id 的部分。

var id_part = json.updates.message.split(" ")[0];
//parse out just the ID number in a group
var re = /\[\[[^:]+:(\d+)\]\]/;
var matches = re.exec(id_part);
var id = matches[1];

然后,要从 references 部分中获取相应的数据,您需要循环遍历,直到找到具有消息中的 id 的数据。这会起作用。

//Ghetto old for loop for browser compatibility
for (var i = 0; i < updates.references.length; i++) {
    if (updates.references[i].id == id) {
        //We have found the reference we want.
        //Do stuff with that reference.
        break;
    }
}

updates.message is a string, not a JavaScript object. You can tell by the quotes around the whole attribute. JavaScript strings don't have a student property, so you are getting undefined. You can parse out the JSON part from the string with regular expressions and then use JSON.parse() to get the JSON object. However, the student id is also in updates.references[0].id in your example.

To get the student ID, do this:

<% alert(updates.references[0].id) %>

edit: If you are really want to get the id out of the message, you need to parse it out somehow. If the message format will always be the same, you can try a regular expression or string splitting to get the part containing the id.

var id_part = json.updates.message.split(" ")[0];
//parse out just the ID number in a group
var re = /\[\[[^:]+:(\d+)\]\]/;
var matches = re.exec(id_part);
var id = matches[1];

To then get the corresponding data out of the references part, you need to loop through until you find one with the id from the message. This would work.

//Ghetto old for loop for browser compatibility
for (var i = 0; i < updates.references.length; i++) {
    if (updates.references[i].id == id) {
        //We have found the reference we want.
        //Do stuff with that reference.
        break;
    }
}
眼泪淡了忧伤 2024-12-09 01:40:06

尝试

var json = {
    "updates": {
        "message": "[[student:123]] is present."
    },
    "references": [
        {
            "type": "student",
            "full_name": "XYZ",
            "id": 123
        }
    ]
};

alert(json.references[0].full_name);

try

var json = {
    "updates": {
        "message": "[[student:123]] is present."
    },
    "references": [
        {
            "type": "student",
            "full_name": "XYZ",
            "id": 123
        }
    ]
};

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