简体   繁体   English

node.js正确扩展json对象

[英]node.js correct extending of json objects

I'm currently using node-ews in a project to access an Exchange Web Server via node.js. 我当前在项目中使用node-ews通过node.js访问Exchange Web Server。 Now i ran into a weird problem. 现在我遇到一个奇怪的问题。 To run for example a "CreateItem" request, which can be an E-Mail or Appointment for example, I'm giving the function the arguments as a json similar to this 例如,要运行“ CreateItem”请求(例如可以是电子邮件或约会),我将给函数提供类似于json的参数

var args = {
    "attributes" : {
        "SendMeetingInvitations" : "SendToAllAndSaveCopy"
    },
    "SavedItemFolderId": {
        "DistinguishedFolderId": {
            "attributes": {
                "Id": "calendar"
            }
        }
    },
    "Items" : {
        "CalendarItem" : {
            "Subject" : "",
            "Body" : {
                "attributes" : {
                },
                "$value" : ""
            },
            "ReminderIsSet" : "true",
            "ReminderMinutesBeforeStart" : "30",
            "Start" : "",
            "End" : "",
            "IsAllDayEvent" : "false",
            "LegacyFreeBusyStatus" : "Busy",
            "Location" : ""
        }
    }
};

As the REST-API I'm writing will receive Attributes like Subject, Start, End etc. I initially stripped out those out of the JSON and would define them later like 作为我正在编写的REST-API,我将收到诸如Subject,Start,End等属性。我最初将这些属性从JSON中剔除,稍后将对其进行定义,例如

args.Items.CalendarItem.Subject = req.body.Subject;

Oddly this will make the internal validation of node-ews fail and tell me that CalendarItem has an invalid Child Subject. 奇怪的是,这将使节点ews的内部验证失败,并告诉我CalendarItem具有无效的子主题。 If i leave Subject als an empty string in the initial args and later change it set it to req.body.Subject it works just fine. 如果我将Subject als留在初始args中为空字符串,然后将其更改为req.body.Subject,则效果很好。

My question is this: Is the Object somewhat different if I later add attributes and if yes is there a way to do it right? 我的问题是:如果以后添加属性,并且是否有正确的方法,则对象是否有些不同? Because I don't think its the best way to have a bunch of empty Attributes in my Object if they aren't used and define standard values for all of them even if api won't require them to be sent. 因为我不认为最好的方法是在我的对象中使用一堆空属性(即使不使用api也不要求发送),并且为所有这些属性定义标准值。

Would great if someone knew the answer. 如果有人知道答案会很好。 Hope i could clarify what the problem is 希望我能澄清问题是什么

Ok, 好,

so the problem seems to be this. 所以问题似乎是这样。 Internally the JSON "object" seems to have an order based on the order the variable is defined. 在内部,JSON“对象”似乎具有基于变量定义顺序的顺序。 in JavaScript this is no problem. 在JavaScript中这没问题。 But when the xml is parsed, the tag that was defined last will also be at the end of the xml so if you had 但是,当解析xml时,最后定义的标签也将位于xml的末尾,因此如果您有

   <someTag>
     <variable2 />
     <variable3 />
   </someTag>

and you add variable1 to your json by someTag.variable1 = x in the end the xml will look like this after beeing parsed by node-ews 然后通过someTag.variable1 = x将variable1添加到您的json中,最后,在通过node-ews解析后,xml看起来像这样

   <someTag>
     <variable2 />
     <variable3 />
     <variable1 >x</variable1>
   </someTag>

Now unfortunately the exchange web server seems to be picky about the order of the xml tags. 现在,不幸的是,Exchange Web服务器似乎对xml标记的顺序有些挑剔。 so when you build your json be sure to use the direct order. 因此,在构建json时,请务必使用直接命令。 Changing content of the json later will not affect the order. 以后更改json的内容不会影响顺序。

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

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