【问题标题】:JSON to AS3 Object issueJSON 到 AS3 对象问题
【发布时间】:2014-05-15 04:07:38
【问题描述】:

我是 JSON to AS3 Objects 的新手,在尝试创建我可以引用的 AS3 对象时遇到问题。这是 JSON:

{
demo: {
    Male: {
        21-30: 2,
        31-40: 0,
        41-50: 0,
        51-60: 0,
        61-70: 0,
        71-80: 0,
        81+: 0
        },
        Female: {
        21-30: 7,
        31-40: 0,
        41-50: 0,
        51-60: 0,
        61-70: 0,
        71-80: 0,
        81+: 0
        }
    },
    days: 0

}

解析代码如下:

        var JSONRequest: URLRequest =  new URLRequest();
    JSONRequest.method      = URLRequestMethod.POST;

    JSONRequest.url         = "https://www.urlhere.com;
    var loader: URLLoader = new URLLoader();
    loader.addEventListener(Event.COMPLETE, handleResponse);
    loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
    loader.load(JSONRequest);

    function handleResponse(event:Event):void{
        var returnData:String = loader.data;
        var parsedData:Object = JSON.parse(returnData);
    }

我已经尝试并成功地使用 for 循环遍历对象,但我不想这样做,我希望能够通过访问点语法中的属性来以对象或数组的形式访问数据。 Object[0].property 等...

真正棘手的部分是我不知道数据嵌套有多大或有多深。我这里加的很简单。

这更像是我将得到的:

    {
    products: {
        Home & Garden: {
            Kitchen: {
                202: {
                    brand: "OXO",
                    description: "12 piece locktop container set",
                    descriptionLong: "Prepping, cooking and cleaning",
                    listPrice: "36.32",
                    sku: "925776",
                    upc: "719812032528"
                    },
                    238: {
                    brand: "Excalibur",
                    description: "Excalibur 2400 4-Tray Dehydrator",
                    descriptionLong: "Dehydration is the healthiest",
                    listPrice: "168.54",
                    sku: "947741",
                    upc: "029743240009"
                    },
                    352: {
                    brand: "Nostalgia",
                    description: "OldFashioned Kettle Corn Maker",
                    descriptionLong: "With the Nostalgia Electrics ",
                    listPrice: "35.49",
                    sku: "925843",
                    upc: "082677300218"
                    },
                    370: {
                    brand: "Joseph Joseph",
                    description: "Nest Plus Measuring (Set of 5 Cups - Multi Coloured)",
                    descriptionLong: "Nest™ Cups are a range of 5",
                    listPrice: "2.46",
                    sku: "926733",
                    upc: "5028420400342"
                    },
                    605: {
                    brand: "Nostalgia",
                    description: "Margarator-Frozen Drink Maker",
                    descriptionLong: "Mix up great-tasting margaritas",
                    listPrice: "140.68",
                    sku: "925851",
                    upc: "082677135889"
                    }
                    },
                Housewares: {
                    206: {
                    brand: "Dyson",
                    description: "Dyson DC44 Animal",
                    descriptionLong: "DC44 Animal has a detachable",
                    listPrice: "406.51",
                    sku: "922846",
                    upc: "879957006362"
                }
    }
}

我还要补充一点,我可以请求我收到的 JSON 的格式,所以如果有更好的方法来格式化来自服务器的数据,我愿意接受。

任何帮助都会很棒。

【问题讨论】:

  • 使用您发布的代码,您已经可以执行 parsedData.products['Home & Garden'].Kitchen... 等。您遇到了什么问题?
  • 您发布的所有 JSON 均无效,并且您没有关闭 ActionScript 中第 4 行的 " 引号。
  • “我不知道数据嵌套有多大或多深” 表示您事先不知道架构。如果您不知道数据的架构,则检索所需内容的唯一方法是遍历每个分支。
  • @Vesper 好吧,他可以创建一个“点语法解析器”,使他能够在运行时使用文本输入查看 JSON,但我不认为他应该,甚至想要这样做。

标签: javascript json actionscript-3 flash


【解决方案1】:

我希望能够通过访问点语法中的属性来以对象或数组的形式访问数据。

在解析调用之前使用JSON.stringifyreplace 回调来重命名键,以便您可以通过点符号访问它们:

function newkey()
  {
  return "key" + Number(Math.random() * 1000).toFixed() + RegExp.$1
  }

//Stringify JSON
var foo = JSON.stringify(parsedData);

/*
Replace numeric keys with a random number without replacing the delimiter
Replace spaces with underscores
*/
var bar = foo.replace(/\d+("?:)/g, newkey).replace(/\s/g,"_")

//Parse resulting string
var baz = JSON.parse(bar);

参考文献

【讨论】:

    猜你喜欢
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 2021-07-21
    • 2021-10-23
    • 2011-02-20
    • 1970-01-01
    相关资源
    最近更新 更多