• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript xml2json.toJson函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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')
})
开发者ID:miggame,项目名称:wechaty,代码行数:28,代码来源:message-raw-payload-parser.spec.ts


示例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
}
开发者ID:miggame,项目名称:wechaty,代码行数:35,代码来源:friendship-event-message-parser.ts


示例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
}
开发者ID:miggame,项目名称:wechaty,代码行数:27,代码来源:friendship-raw-payload-parser.ts


示例4: request

	request(url, (err, response, xml) => {
		if (err) {
			console.error(err);
			return;
		}

		res.send(xml2json.toJson(xml));
	});
开发者ID:syuilo,项目名称:misskey-core,代码行数:8,代码来源:rss-proxy.ts


示例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;
    }

  }
开发者ID:Marabyte,项目名称:spa-static,代码行数:19,代码来源:urlExtractor.ts


示例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
});
开发者ID:Kroisse,项目名称:DefinitelyTyped,代码行数:24,代码来源:xml2json-tests.ts


示例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
//.........这里部分代码省略.........
开发者ID:miggame,项目名称:wechaty,代码行数:101,代码来源:room-event-join-message-parser.ts


示例8: fromXml

export function fromXml(xml) { return JSON.parse(parser.toJson(xml)); }
开发者ID:yedf,项目名称:wx-rest,代码行数:1,代码来源:util2.ts


示例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
    //   })
    // })
  }
开发者ID:miggame,项目名称:wechaty,代码行数:83,代码来源:bridge.ts



注:本文中的xml2json.toJson函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript xmlbuilder.create函数代码示例发布时间:2022-05-25
下一篇:
TypeScript xml2js.Parser类代码示例发布时间:2022-05-25
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap