将xml请求转换为具有重复节点名称的json请求

时间:2018-04-09 10:27:34

标签: javascript json

我尝试将数据推送到第三方Web服务,特别是将xml请求转换为json请求(用于节点soap)。

以下是原始xml请求的示例:

error: bundling failed: Error: Unable to resolve module `ExamplePackage` from `/Users/vikasagr/workspace/test/ReactNative/MyPackage/src/index.js`: Module `ExamplePackage` does not exist in the Haste module map

This might be related to https://github.com/facebook/react-native/issues/4968
To resolve try the following:
  1. Clear watchman watches: `watchman watch-del-all`.
  2. Delete the `node_modules` folder: `rm -rf node_modules && npm install`.
  3. Reset Metro Bundler cache: `rm -rf /tmp/metro-bundler-cache-*` or `npm start -- --reset-cache`.  4. Remove haste cache: `rm -rf /tmp/haste-map-react-native-packager-*`.
    at ModuleResolver.resolveDependency (/Users/vikasagr/workspace/test/ReactNative/MyPackage/node_modules/metro/src/node-haste/DependencyGraph/ModuleResolution.js:161:1460)
    at ResolutionRequest.resolveDependency (/Users/vikasagr/workspace/test/ReactNative/MyPackage/node_modules/metro/src/node-haste/DependencyGraph/ResolutionRequest.js:91:16)
    at DependencyGraph.resolveDependency (/Users/vikasagr/workspace/test/ReactNative/MyPackage/node_modules/metro/src/node-haste/DependencyGraph.js:272:4579)
    at dependencies.map.relativePath (/Users/vikasagr/workspace/test/ReactNative/MyPackage/node_modules/metro/src/DeltaBundler/traverseDependencies.js:376:19)
    at Array.map (<anonymous>)
    at resolveDependencies (/Users/vikasagr/workspace/test/ReactNative/MyPackage/node_modules/metro/src/DeltaBundler/traverseDependencies.js:374:16)
    at /Users/vikasagr/workspace/test/ReactNative/MyPackage/node_modules/metro/src/DeltaBundler/traverseDependencies.js:212:33
    at Generator.next (<anonymous>)
    at step (/Users/vikasagr/workspace/test/ReactNative/MyPackage/node_modules/metro/src/DeltaBundler/traverseDependencies.js:297:313)
    at /Users/vikasagr/workspace/test/ReactNative/MyPackage/node_modules/metro/src/DeltaBundler/traverseDependencies.js:297:473

我遇到的问题是尝试将这些重复节点转换为对象,新对象数据将被最后一个请求覆盖。

这是我到目前为止所拥有的:

<EformData>
            <EformFields>
               <FieldName>txt_customername</FieldName>
               <FieldValue>Scott</FieldValue>
            </EformFields>
            <EformFields>
               <FieldName>txt_organisation</FieldName>
               <FieldValue>My Orginisation</FieldValue>
            </EformFields>
            <EformFields>
               <FieldName>txt_address</FieldName>
               <FieldValue>My Address</FieldValue>
            </EformFields>
            <EformFields>
               <FieldName>txt_telnumber</FieldName>
               <FieldValue>123456</FieldValue>
            </EformFields>
</EformData>

如下所示,只有最后一个请求存储在对象中,其他请求被覆盖:

var formValues = {
    "txt_customername": "Scott",
    "txt_organisation": "My Orginisation",
    "txt_address": "My Address",
    "txt_telnumber": "123456"
}

// Container
var EformData = { 
        "EformFields": {
        }  
};

// populate the object
for (var key in formValues) {            
    EformData.EformFields.FieldName = [key];
    EformData.EformFields.FieldValue = formValues[key];
}

是否可以以匹配原始重复xml节点数据的方式构建对象?

2 个答案:

答案 0 :(得分:1)

json的数据结构应该是EformData有一组EformFields个对象,其属性为FieldNameFieldValue

var formValues = {
    "txt_customername": "Scott",
    "txt_organisation": "My Orginisation",
    "txt_address": "My Address",
    "txt_telnumber": "123456"
}

// Container
var EformData = { 
    "EformFields": []  
};

// populate the object
for (var key in formValues) {            
    EformData.EformFields.push({
        "FieldName": key,
        "FieldValue": formValues[key]
    });
}

答案 1 :(得分:0)

在您的数组中,始终只填充第0个索引,因此在下一次迭代时,它会被覆盖,为数组迭代添加索引,如下所示

// Container
var EformData = { 
    "EformFields": [
    ] 
};

// populate the object
int i=0;
for (key in formValues) {            
    EformData.EformFields[i].FieldName = [key];
    EformData.EformFields[i].FieldValue = formValues[key];
    i++;
}