简体   繁体   English

遍历复杂的JSON对象

[英]Looping over Complex JSON Object

I have created an application that accepts faxes in xml format and converts them to json objects so that I can take what I need from the fax which is the base64 string located in the "file contents" variable within the document. 我创建了一个应用程序,该应用程序可以接受xml格式的传真并将其转换为json对象,这样我就可以从传真中获取所需的信息,传真是位于文档中“文件内容”变量中的base64字符串。

This is the code that I have to do that. 这是我要做的代码。

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

This is good if there is only one field labeled file contents. 如果只有一个标记为文件内容的字段,那么这很好。 Now I am trying to alter my code to check for a variable amount of base64 strings and return them all for storing them in the file system. 现在,我试图更改代码以检查可变数量的base64字符串,并将它们全部返回以将其存储在文件系统中。 The xml documents always look the same but I can't seem to figure out how to make a loop that will check for variable amounts of base64 strings. xml文档总是看起来一样,但我似乎无法弄清楚如何制作一个循环来检查可变数量的base64字符串。 This is what the document looks like. 这就是文档的样子。

<?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>

I am trying to make the code go through all of the PageContents and store all of those base64 images in the database in a sane way. 我试图使代码遍历所有PageContents,并以理智的方式将所有这些base64图像存储在数据库中。 Any Help Greatly Appreciated. 非常感谢任何帮助。

You can do a for loop: 您可以执行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
}

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

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