简体   繁体   English

为JSON创建嵌套对象

[英]Create Nested Objects for JSON

I'd like to achieve the following JSON result/JS object: 我想实现以下JSON结果/ JS对象:

{
    "mysex": "female",
    "yoursex": "male",
    "location": {
        "lat": "48",
        "lng": "1"
    },
    "description": "descr2",
    "owner": "zBYnfuu8DXEwMttwZ",
    "nickname": "user",
    "_id": "xcGAkaANcmJP9jQu9"
}

I already have a message object which gets all other information besides the location nested object values from a form. 我已经有一个消息对象,除了表单中的位置嵌套对象值之外,还获取所有其他信息。 That works fine. 这很好。

var message = $('#daform').serializeJSON();

I already have the lat and lng values stored as variables 'templat' and 'templng' but I don't know the syntax I need to "push" a nested object in message. 我已经将lat和lng值存储为变量'templat'和'templng',但我不知道我需要在消息中“推送”嵌套对象的语法。

Assuming that var message = $('#daform').serializeJSON(); 假设var message = $('#daform').serializeJSON(); is equivalent to 相当于

{
    "mysex": "female",
    "yoursex": "male",
    "description": "descr2",
    "owner": "zBYnfuu8DXEwMttwZ",
    "nickname": "user",
    "_id": "xcGAkaANcmJP9jQu9"
}

Just add 只需添加

message.location = { 'lat': templat, 'lng': templng };

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

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