本文整理汇总了TypeScript中xml2json.toJson函数的典型用法代码示例。如果您正苦于以下问题:TypeScript toJson函数的具体用法?TypeScript toJson怎么用?TypeScript toJson使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了toJson函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: messageRawPayloadParser
test.skip('attachment file with ext .xlsx', async t => {
const MESSAGE_PAYLOAD: PadchatMessagePayload = {
content : '<msg><appmsg appid="" sdkver="0"><title>面试--运营助理.xlsx</title><des></des><action></action><type>6</type><showtype>0</showtype><mediatagname></mediatagname><messageaction></messageaction><content></content><url></url><lowurl></lowurl><dataurl></dataurl><lowdataurl></lowdataurl><appattach><totallen>29001</totallen><attachid>@cdn_304f0201000448304602010002040592f70202033d0af802046631feb602045b235b5f0421777869645f7a6a3263616870777a67696531323136365f313532393034333830370204010400050201000400_abee7526f03e4d598aee3f36a9f6cf87_1</attachid><emoticonmd5></emoticonmd5><fileext>xlsx</fileext><cdnattachurl>304f0201000448304602010002040592f70202033d0af802046631feb602045b235b5f0421777869645f7a6a3263616870777a67696531323136365f313532393034333830370204010400050201000400</cdnattachurl><aeskey>abee7526f03e4d598aee3f36a9f6cf87</aeskey><encryver>0</encryver></appattach><extinfo></extinfo><sourceusername></sourceusername><sourcedisplayname></sourcedisplayname><commenturl></commenturl><thumburl></thumburl><md5>73dd2e7c3ec58bbae471dc2d6374578a</md5></appmsg><fromusername>qq512436430</fromusername><scene>0</scene><appinfo><version>1</version><appname></appname></appinfo><commenturl></commenturl></msg>',
continue : 1,
description : 'wechaty-alias : [文件]面试--运营助理.xlsx',
from_user : 'qq512436430',
msg_id : '7844942963127630689',
msg_source : '<msgsource />\n',
msg_type : 5,
status : 1,
sub_type : 49,
timestamp : 1529043807,
to_user : 'wxid_zj2cahpwzgie12',
uin : 324216852,
}
const EXPECTED_PAYLOAD: MessagePayload = {
//
} as any
const payload = messageRawPayloadParser(MESSAGE_PAYLOAD)
console.log(payload)
const { toJson } = require('xml2json')
console.log(JSON.parse(toJson(payload.text)))
t.deepEqual(payload, EXPECTED_PAYLOAD, 'should parse share card message peer to peer')
})
示例2: friendshipReceiveEventMessageParser
export function friendshipReceiveEventMessageParser(
rawPayload: PadchatMessagePayload,
): null | string {
if (!isPayload(rawPayload)) {
return null
}
interface XmlSchema {
msg: {
fromusername: string,
encryptusername: string,
content: string,
ticket: string,
}
}
try {
const jsonPayload: XmlSchema = JSON.parse(
toJson(
rawPayload.content,
),
)
const contactId = jsonPayload.msg.fromusername
if (isContactId(contactId)) {
return contactId
}
} catch (e) {
// not receive event
}
return null
}
示例3: friendshipRawPayloadParserReceive
function friendshipRawPayloadParserReceive(
rawPayload: PadchatMessagePayload,
) {
const tryXmlText = rawPayload.content
interface XmlSchema {
msg?: PadchatFriendshipPayload,
}
const jsonPayload: XmlSchema = toJson(tryXmlText, { object: true })
if (!jsonPayload.msg) {
throw new Error('no msg found')
}
const padchatFriendshipPayload: PadchatFriendshipPayload = jsonPayload.msg
const friendshipPayload: FriendshipPayloadReceive = {
id : rawPayload.msg_id,
contactId : padchatFriendshipPayload.fromusername,
hello : padchatFriendshipPayload.content,
stranger : padchatFriendshipPayload.encryptusername,
ticket : padchatFriendshipPayload.ticket,
type : FriendshipType.Receive,
}
return friendshipPayload
}
示例4: request
request(url, (err, response, xml) => {
if (err) {
console.error(err);
return;
}
res.send(xml2json.toJson(xml));
});
示例5: getUrlList
public async getUrlList() {
let urlList: string[] = [];
try {
const sitemap = await this.getSitemap();
let sitemapObj = xmlParser.toJson(sitemap, { object: true });
let urlArray = sitemapObj.urlset.url;
for (let url of urlArray) {
if (url.loc) {
urlList.push(url.loc);
}
}
return urlList;
} catch (error) {
console.error(`async caught an error: ${error}`);
throw error;
}
}
示例6:
import * as parser from 'xml2json';
var xml = "<foo attr=\"value\">bar</foo>";
// xml to json
var jsonString: string = parser.toJson(xml);
// json to xml
var xml: string = parser.toXml(jsonString);
// xml to json in object mode and JsonOptions
var jsonObject: {} = parser.toJson(xml, {
object: true,
reversible: false,
coerce: false,
sanitize: true,
trim: true,
arrayNotation: false
});
// json to xml with XmlOptions
var xml: string = parser.toXml(jsonObject, {
sanitize: true
});
示例7: roomJoinEventMessageParser
export function roomJoinEventMessageParser(
rawPayload: PadchatMessagePayload,
): null | PuppetRoomJoinEvent {
if (!isPayload(rawPayload)) {
return null
}
const roomId = rawPayload.from_user
if (!isRoomId(roomId)) {
return null
}
let content = rawPayload.content
/**
* when the message is a Recalled type, bot can undo the invitation
*/
if (rawPayload.sub_type === PadchatMessageType.Recalled) {
/**
* content:
* ```
* 3453262102@chatroom:
* <sysmsg type="delchatroommember">
* ...
* </sysmsg>
* ```
*/
const tryXmlText = content.replace(/^[^\n]+\n/, '')
interface XmlSchema {
sysmsg: {
type: 'revokemsg' | 'delchatroommember',
delchatroommember?: {
plain : string,
text : string,
},
revokemsg?: {
replacemsg : string,
msgid : string,
newmsgid : string,
session : string,
},
}
}
const jsonPayload = toJson(tryXmlText, { object: true }) as XmlSchema
try {
if (jsonPayload.sysmsg.type === 'delchatroommember') {
content = jsonPayload.sysmsg.delchatroommember!.plain
} else if (jsonPayload.sysmsg.type === 'revokemsg') {
content = jsonPayload.sysmsg.revokemsg!.replacemsg
} else {
throw new Error('unknown jsonPayload sysmsg type: ' + jsonPayload.sysmsg.type)
}
} catch (e) {
console.error(e)
console.log('jsonPayload:', jsonPayload)
throw e
}
}
let matchesForBotInviteOtherEn = null as null | string[]
let matchesForOtherInviteBotEn = null as null | string[]
let matchesForOtherInviteOtherEn = null as null | string[]
let matchesForOtherInviteOtherQrcodeEn = null as null | string[]
let matchesForBotInviteOtherZh = null as null | string[]
let matchesForOtherInviteBotZh = null as null | string[]
let matchesForOtherInviteOtherZh = null as null | string[]
let matchesForOtherInviteOtherQrcodeZh = null as null | string[]
ROOM_JOIN_BOT_INVITE_OTHER_REGEX_LIST_EN.some(
regex => !!(matchesForBotInviteOtherEn = content.match(regex)),
)
ROOM_JOIN_OTHER_INVITE_BOT_REGEX_LIST_EN.some(
regex => !!(matchesForOtherInviteBotEn = content.match(regex)),
)
ROOM_JOIN_OTHER_INVITE_OTHER_REGEX_LIST_EN.some(
regex => !!(matchesForOtherInviteOtherEn = content.match(regex)),
)
ROOM_JOIN_OTHER_INVITE_OTHER_QRCODE_REGEX_LIST_EN.some(
regex => !!(matchesForOtherInviteOtherQrcodeEn = content.match(regex)),
)
ROOM_JOIN_BOT_INVITE_OTHER_REGEX_LIST_ZH.some(
regex => !!(matchesForBotInviteOtherZh = content.match(regex)),
)
ROOM_JOIN_OTHER_INVITE_BOT_REGEX_LIST_ZH.some(
regex => !!(matchesForOtherInviteBotZh = content.match(regex)),
)
ROOM_JOIN_OTHER_INVITE_OTHER_REGEX_LIST_ZH.some(
regex => !!(matchesForOtherInviteOtherZh = content.match(regex)),
)
ROOM_JOIN_OTHER_INVITE_OTHER_QRCODE_REGEX_LIST_ZH.some(
regex => !!(matchesForOtherInviteOtherQrcodeZh = content.match(regex)),
)
const matchesForBotInviteOther = matchesForBotInviteOtherEn || matchesForBotInviteOtherZh
const matchesForOtherInviteBot = matchesForOtherInviteBotEn || matchesForOtherInviteBotZh
const matchesForOtherInviteOther = matchesForOtherInviteOtherEn || matchesForOtherInviteOtherZh
const matchesForOtherInviteOtherQrcode = matchesForOtherInviteOtherQrcodeEn || matchesForOtherInviteOtherQrcodeZh
//.........这里部分代码省略.........
示例8: fromXml
export function fromXml(xml) { return JSON.parse(parser.toJson(xml)); }
示例9: testBlockedMessage
/**
* Throw if there's a blocked message
*/
public async testBlockedMessage(text?: string): Promise<string | false> {
if (!text) {
text = await this.innerHTML()
}
if (!text) {
throw new Error('testBlockedMessage() no text found!')
}
const textSnip = text.substr(0, 50).replace(/\n/, '')
log.verbose('PuppetPuppeteerBridge', 'testBlockedMessage(%s)',
textSnip)
interface BlockedMessage {
error?: {
ret : number,
message : string,
}
}
let obj: BlockedMessage
try {
// see unit test for detail
const tryXmlText = this.preHtmlToXml(text)
obj = JSON.parse(toJson(tryXmlText))
} catch (e) {
log.warn('PuppetPuppeteerBridge', 'testBlockedMessage() toJson() exception: %s', e)
return false
}
if (!obj) {
// FIXME: when will this happen?
log.warn('PuppetPuppeteerBridge', 'testBlockedMessage() toJson(%s) return empty obj', textSnip)
return false
}
if (!obj.error) {
return false
}
const ret = +obj.error.ret
const message = obj.error.message
log.warn('PuppetPuppeteerBridge', 'testBlockedMessage() error.ret=%s', ret)
if (ret === 1203) {
// <error>
// <ret>1203</ret>
// <message>当前登录环境异常。为了你的帐号安全,暂时不能登录web微信。你可以通过手机客户端或者windows微信登录。</message>
// </error>
return message
}
return message // other error message
// return new Promise<string | false>(resolve => {
// parseString(tryXmlText, { explicitArray: false }, (err, obj: BlockedMessage) => {
// if (err) { // HTML can not be parsed to JSON
// return resolve(false)
// }
// if (!obj) {
// // FIXME: when will this happen?
// log.warn('PuppetPuppeteerBridge', 'testBlockedMessage() parseString(%s) return empty obj', textSnip)
// return resolve(false)
// }
// if (!obj.error) {
// return resolve(false)
// }
// const ret = +obj.error.ret
// const message = obj.error.message
// log.warn('PuppetPuppeteerBridge', 'testBlockedMessage() error.ret=%s', ret)
// if (ret === 1203) {
// // <error>
// // <ret>1203</ret>
// // <message>当前登录环境异常。为了你的帐号安全,暂时不能登录web微信。你可以通过手机客户端或者windows微信登录。</message>
// // </error>
// return resolve(message)
// }
// return resolve(message) // other error message
// })
// })
}