将coldfusion json转换为struct

时间:2013-04-02 21:45:11

标签: json coldfusion struct cfml

我有一个来自jQuery应用程序的JSON变量输出,如下所示:

[{"id":1},
{"id":197,"children":[{"id":198},{"id":199},{"id":200}]},
{"id":2,"children":[{"id":3},{"id":4},{"id":143},{"id":6},{"id":5},{"id":7},{"id":8},{"id":9},{"id":10},{"id":11},{"id":12}]},
{"id":15,"children":[{"id":17},{"id":190},{"id":191},{"id":131},{"id":16},{"id":142},{"id":124}]},
{"id":149,"children":[{"id":150},{"id":160},{"id":161},{"id":184}]},
{"id":20,"children":[{"id":132},{"id":127},{"id":152},{"id":107},{"id":108},{"id":109},{"id":110},{"id":125},{"id":128},{"id":130},{"id":129},{"id":112}]},
{"id":162,"children":[{"id":163},{"id":164},{"id":165},{"id":166},{"id":186},{"id":187}]},
{"id":167,"children":[{"id":173},{"id":168},{"id":170},{"id":169},{"id":192},{"id":194}]},
{"id":174,"children":[{"id":176},{"id":175},{"id":188}]},
{"id":153,"children":[{"id":178},{"id":179},{"id":195},{"id":154},{"id":157}]},
{"id":180,"children":[{"id":181},{"id":182},{"id":183},{"id":189}]},
{"id":122},
{"id":21},
{"id":13},
{"id":92},
{"id":22},
{"id":113},
{"id":114}]

我需要将此变量转换为结构,然后通过第一个id参数循环结构,以查找每个id的子项(如果有的话)。我试图将其转换为类似的结构,但我收到错误:

<cfset jsonarray = deserializeJson(output)>
<cfloop collection="#jsonarray#" index="id">
<cfdump var="#jsonarray[id]#">
</cfloop>

它不会将变量识别为结构:

Invalid collection [{id={1}}, {children={[{id={198}}, {id={199}}, {id={200}}]},id={197}}, {children={[{id={3}}, {id={143}}, {id={4}}, {id={6}}, {id={5}}, {id={7}}, {id={8}}, {id={9}}, {id={10}}, {id={11}}, {id={12}}]},id={2}}, {children={[{id={17}}, {id={190}}, {id={191}}, {id={131}}, {id={16}}, {id={142}}, {id={124}}]},id={15}}, {children={[{id={150}}, {id={160}}, {id={161}}, {id={184}}]},id={149}}, {children={[{id={132}}, {id={127}}, {id={152}}, {id={107}}, {id={108}}, {id={109}}, {id={110}}, {id={125}}, {id={128}}, {id={130}}, {id={129}}, {id={112}}]},id={20}}, {children={[{id={163}}, {id={164}}, {id={165}}, {id={166}}, {id={186}}, {id={187}}]},id={162}}, {children={[{id={173}}, {id={168}}, {id={170}}, {id={169}}, {id={192}}, {id={194}}]},id={167}}, {children={[{id={176}}, {id={175}}, {id={188}}]},id={174}}, {children={[{id={178}}, {id={179}}, {id={195}}, {id={154}}, {id={157}}]},id={153}}, {children={[{id={181}}, {id={182}}, {id={183}}, {id={189}}]},id={180}}, {id={122}}, {id={21}}, {id={13}}, {id={92}}, {id={22}}, {id={113.... Must be a valid structure or COM object. 

3 个答案:

答案 0 :(得分:13)

JSON中,[]表示数组,{}表示结构(或对象)。所以你的输入实际上是一个结构数组。您需要使用array循环,而不是集合循环:

<cfset arrayOfStructs = deserializeJson(output)>
<cfloop array="#arrayOfStructs#" index="parent">
      <cfset parentID = parent.id />
      ... 
</cfloop>

children也是一个结构数组。在外部循环内部,检查该密钥是否存在。如果找到,则遍历子数组并对每个id执行一些操作:

  <cfif structKeyExists(parent, "children")>
      <cfloop array="#parent.children#" index="child">
          ...
      </cfloop>
  </cfif>

答案 1 :(得分:1)

整洁的cfscript版本。 :)

<cfscript>
    structObj = deserializeJson(jsonString);

    for(i = 1; i LTE ArrayLen(structObj); i++){

        WriteOutput("parent id : " & structObj[i].id & "<br>"); 
        if(StructKeyExists(structObj[i], "children")){
            for(j = 1; j LTE ArrayLen(structObj[i].children); j++){
                WriteOutput("    -children id : " & structObj[i].children[j].id & "<br>");
            }
        }
    }
</cfscript>

答案 2 :(得分:1)

我创建了一个Angular 1.4 ColdFusion 9 JSON Normalizer here

 var myURL = 'myCfc.cfc?method=getItemsFromDb';
 var app = angular.module('angularOutput',[]);
 app.controller("sectionController", function($scope, $http) {
      $http.get(myURL).
        success(function(data, status, headers, config) {
          var log = []; 
          var output = '';
          angular.forEach(data.DATA, function(value, key) {
                    this.push(output +='{"value": ');
                    this.push(output += '"'+value[0]+'"');
                    this.push(output +=',"text":');
                    this.push(output += '"'+value[1]+'"');
                    this.push(output +='}');
                    this.push(output +=',');
                }, log);
              output = output.replace(/,\s*$/, "");/*had to remove the final comma */
              output = '['+output+']'; /*had to add the [] to corectally form the output*/

          $scope.sections = angular.fromJson(output);
        }).
        error(function(data, status, headers, config) {
          console.log(data);
        });
    });