循环复杂的JSON对象

时间:2014-10-22 17:08:53

标签: javascript json node.js

我创建了一个接受xml格式传真的应用程序,并将它们转换为json对象,以便我可以从传真中获取所需的内容,即位于"文件内容中的base64字符串"文档中的变量。

这是我必须做的代码。

exports.receiveFax = function(req, res){
       var form = new formidable.IncomingForm();
           form.keepExtensions = true;
           form.parse(req, function(err, fields, files) { 
           parseString(fields.xml, function (err, result) {
              base64 = result['InboundPostRequest']['FaxControl'][0]['FileContents'];
              ....
              ....
              //then we decode 'base64 and save it to the database

如果只有一个字段标记为文件内容,那么这很好。现在我正在尝试更改我的代码以检查可变数量的base64字符串并将它们全部返回以将它们存储在文件系统中。 xml文档总是看起来一样,但我似乎无法弄清楚如何创建一个循环来检查可变数量的base64字符串。这就是文档的样子。

<?xml version="1.0"?>
<InboundPostRequest>
  <AccessControl>
    <UserName>test</UserName>
    <Password>test</Password>
  </AccessControl>
  <RequestControl>
    <RequestDate>08/18/2005 12:07:53</RequestDate>
    <RequestType>New Inbound</RequestType>
  </RequestControl>
  <FaxControl>
    <AccountID>1234567890</AccountID>
    <NumberDialed>0987654321</NumberDialed>
    <DateReceived>08/18/2005 12:07:49</DateReceived>
    <FaxName>SampleOut</FaxName>
    <FileType>pdf</FileType>
    <PageCount>5</PageCount>
    <CSID>8587123600</CSID>
    <ANI>8587123600</ANI>
    <Status>0</Status>
    <MCFID>12345678</MCFID>
    <UserFieldControl>
      <UserFieldsRead>2</UserFieldsRead>
      <UserFields>
        <UserField>
          <FieldName>Customer Name</FieldName>
          <FieldValue>John Doe</FieldValue>
        </UserField>
        <UserField>
          <FieldName>PIN Number</FieldName>
          <FieldValue>1234</FieldValue>
        </UserField>
      </UserFields>
    </UserFieldControl>
    <BarcodeControl>
      <BarcodesRead>5</BarcodesRead>
      <Barcodes>
        <Barcode>
          <Key>EFAXTEST1A</Key>
          <AdditionalInfo>
            <ReadSequence>1</ReadSequence>
            <ReadDirection>2-Dimentional</ReadDirection>
            <Symbology>DATAMATRIX</Symbology>
            <CodeLocation>
              <PageNumber>1</PageNumber>
              <PageCoordinates>
                <StartEdge>
                  <XStartPointA>278.0</XStartPointA>
                  <YStartPointA>577.0</YStartPointA>
                  <XStartPointB>278.0</XStartPointB>
                  <YStartPointB>299.0</YStartPointB>
                </StartEdge>
                <EndEdge>
                  <XEndPointA>545.0</XEndPointA>
                  <YEndPointA>299.0</YEndPointA>
                  <XEndPointB>545.0</XEndPointB>
                  <YEndPointB>577.0</YEndPointB>
                </EndEdge>
              </PageCoordinates>
            </CodeLocation>
          </AdditionalInfo>
        </Barcode>
        <Barcode>
          <Key>EFAXTEST2A</Key>
          <AdditionalInfo>
            <ReadSequence>1</ReadSequence>
            <ReadDirection>2-Dimentional</ReadDirection>
            <Symbology>DATAMATRIX</Symbology>
            <CodeLocation>
              <PageNumber>2</PageNumber>
              <PageCoordinates>
                <StartEdge>
                  <XStartPointA>278.0</XStartPointA>
                  <YStartPointA>600.0</YStartPointA>
                  <XStartPointB>278.0</XStartPointB>
                  <YStartPointB>320.0</YStartPointB>
                </StartEdge>
                <EndEdge>
                  <XEndPointA>545.0</XEndPointA>
                  <YEndPointA>320.0</YEndPointA>
                  <XEndPointB>545.0</XEndPointB>
                  <YEndPointB>600.0</YEndPointB>
                </EndEdge>
              </PageCoordinates>
            </CodeLocation>
          </AdditionalInfo>
        </Barcode>
        <Barcode>
          <Key>EFAXTEST3A</Key>
          <AdditionalInfo>
            <ReadSequence>1</ReadSequence>
            <ReadDirection>2-Dimentional</ReadDirection>
            <Symbology>DATAMATRIX</Symbology>
            <CodeLocation>
              <PageNumber>3</PageNumber>
              <PageCoordinates>
                <StartEdge>
                  <XStartPointA>277.0</XStartPointA>
                  <YStartPointA>608.0</YStartPointA>
                  <XStartPointB>277.0</XStartPointB>
                  <YStartPointB>328.0</YStartPointB>
                </StartEdge>
                <EndEdge>
                  <XEndPointA>545.0</XEndPointA>
                  <YEndPointA>328.0</YEndPointA>
                  <XEndPointB>545.0</XEndPointB>
                  <YEndPointB>608.0</YEndPointB>
                </EndEdge>
              </PageCoordinates>
            </CodeLocation>
          </AdditionalInfo>
        </Barcode>
        <Barcode>
          <Key>EFAXTEST4A</Key>
          <AdditionalInfo>
            <ReadSequence>1</ReadSequence>
            <ReadDirection>2-Dimentional</ReadDirection>
            <Symbology>DATAMATRIX</Symbology>
            <CodeLocation>
              <PageNumber>4</PageNumber>
              <PageCoordinates>
                <StartEdge>
                  <XStartPointA>277.0</XStartPointA>
                  <YStartPointA>608.0</YStartPointA>
                  <XStartPointB>277.0</XStartPointB>
                  <YStartPointB>328.0</YStartPointB>
                </StartEdge>
                <EndEdge>
                  <XEndPointA>545.0</XEndPointA>
                  <YEndPointA>328.0</YEndPointA>
                  <XEndPointB>545.0</XEndPointB>
                  <YEndPointB>608.0</YEndPointB>
                </EndEdge>
              </PageCoordinates>
            </CodeLocation>
          </AdditionalInfo>
        </Barcode>
        <Barcode>
          <Key>EFAXTEST5A</Key>
          <AdditionalInfo>
            <ReadSequence>1</ReadSequence>
            <ReadDirection>2-Dimentional</ReadDirection>
            <Symbology>DATAMATRIX</Symbology>
            <CodeLocation>
              <PageNumber>5</PageNumber>
              <PageCoordinates>
                <StartEdge>
                  <XStartPointA>277.0</XStartPointA>
                  <YStartPointA>608.0</YStartPointA>
                  <XStartPointB>277.0</XStartPointB>
                  <YStartPointB>328.0</YStartPointB>
                </StartEdge>
                <EndEdge>
                  <XEndPointA>545.0</XEndPointA>
                  <YEndPointA>328.0</YEndPointA>
                  <XEndPointB>545.0</XEndPointB>
                  <YEndPointB>608.0</YEndPointB>
                </EndEdge>
              </PageCoordinates>
            </CodeLocation>
          </AdditionalInfo>
        </Barcode>
      </Barcodes>
    </BarcodeControl>
    <PageContentControl>
      <Pages>
        <Page>
          <PageNumber>1</PageNumber>
          <PageContents>//Base64 String Removed For Legibility</PageContents>
        </Page>
        <Page>
          <PageNumber>2</PageNumber>
          <PageContents>//Base64 String Removed For Legibility</PageContents>
        </Page>
        <Page>
          <PageNumber>3</PageNumber>
          <PageContents>//Base64 String Removed For Legibility</PageContents>
        </Page>
        <Page>
          <PageNumber>4</PageNumber>
          <PageContents>//Base64 String Removed For Legibility</PageContents>
        </Page>
        <Page>
          <PageNumber>5</PageNumber>
          <PageContents>//Base64 String Removed For Legibility</PageContents>
        </Page>
      </Pages>  
    </PageContentControl>
  </FaxControl>
</InboundPostRequest>

我正在尝试让代码遍历所有PageContents,并以理智的方式将所有这些base64图像存储在数据库中。任何帮助都非常赞赏。

1 个答案:

答案 0 :(得分:0)

您可以执行for循环:

var pages = result['InboundPostRequest']['FaxControl']['PageContentControl']['Pages']['Page'];
for(var i in pages){
    var pageNum  = pages[i]['PageNumber'];
    var imageData= pages[i]['PageContents'];
    //Do database insert
}