【问题标题】:Read data from json从json读取数据
【发布时间】:2017-09-20 07:00:20
【问题描述】:

这是我的 JSON 文件

{
 "styleMapping": {
  "1": {
  "zIndex": 1,
  "StyleMappingCollection": {
    "636404903145791477": {
      "border": {
        "color": "#FF000000",
        "width": "Small",
        "type": "Solid"
      },
      "background": {
        "bgOption": "Image",
        "thumbOption": "Pointer",
        "opacity": 1.0,
        "bgColor": "#FFF5F5DC",
        "bgImage": "C:\Users\raj\Downloads\images\image.jpg",
      }
 }
}

我想在我的 javascript 中捕获 bgImage 参数。

我的脚本代码是

 $http.get('Data//xyz.json').then(successCallback, errorCallback);

                function successCallback(response)
                {           
                   sliderCtrlPtr.sliderParams = response.data;
                   sliderCtrlPtr.sliderParams.height = response.data.deviceHeight;

                   console.log("After JSON read : ",sliderCtrlPtr.sliderParams);

                }
                function errorCallback(error)
                {
                    //error code                       
                }

                sliderCtrlPtr.GetsliderStyle = function () 
                {
                    if(sliderCtrlPtr.sliderParams != undefined)
                    {
                        var styleObj = sliderCtrlPtr.sliderParams;       
                        canvas.color = styleObj.StyleMappingCollection.
 636404903145791477.background.bgColor;
             }                       
          };
    }]);
})();

我想从脚本中的 json 文件中检索 bgColor 或 bgImage 参数。我该怎么做?

【问题讨论】:

  • 你得到什么回应?或者你什至得到任何?尝试在successCallbackerrorCallback 中使用console.log 并检查一次。
  • 试试 styleObj.StyleMappingCollection[636404903145791477].background.bgColor
  • 你能检查一下你的 JSON 是否正确吗?对我来说似乎是错误的,还是您发布了一半的 JSON?

标签: javascript angularjs json html


【解决方案1】:

我认为应该是。从提供的不完整的JSON中,我只能找到这个错误!

sliderCtrlPtr.GetsliderStyle = function() {
    if (sliderCtrlPtr.sliderParams != undefined) {
      var styleObj = sliderCtrlPtr.sliderParams;
      canvas.color = styleObj["styleMapping"]["1"]["StyleMappingCollection"]
      ["636404903145791477"]["background"]["bgColor"];
      canvas.image = styleObj["styleMapping"]["1"]["StyleMappingCollection"]
      ["636404903145791477"]["background"]["bgImage"];
    }
}

【讨论】:

  • 上面的代码是对的。但问题是硬编码值。我想将 bgColor 值存储在颜色变量中。如何从 json 文件中获取路径,以便我们可以根据需要存储值。所以我们没有硬编码这个值,因为 json 文件总是在变化。
  • @RajeshRamtere 您可以使用$scope.bgColor = styleObj["styleMapping"]["1"]["StyleMappingCollection"] ["636404903145791477"]["background"]["bgImage"];sliderCtrlPtr.bgColor= styleObj["styleMapping"]["1"]["StyleMappingCollection"] ["636404903145791477"]["background"]["bgImage"]; 等赋值运算符将此值分配给任意变量,您遇到什么问题?
  • 如果我的 636404903145791477 现在不同了,那么我该如何检索该参数。考虑我总是得到不同的 json 文件。这个数字也在变化。现在我想动态获取 gata
  • @RajeshRamtere 您需要像这样遍历键。 for( x in styleObj["styleMapping"]["1"]["StyleMappingCollection"]){ sliderCtrlPtr.bgColor = styleObj["styleMapping"]["1"]["StyleMappingCollection"] [x]["background"]["bgColor"]; } 我假设您在问题中只有一个属性“636404903145791477”
  • 不,我有多个键,例如 636404903145791477、63640490314574523、636404903145791123 等,具体取决于用户添加特定按钮的方式。
【解决方案2】:

点符号:

首先你需要解析JSON

obj = JSON.parse(json);

由于 JSON 包含对象,因此您可以使用点表示法,例如

var backColor = obj.StyleMappingCollection.background.bgcolor;

问题是,JSON 中的那些数字字段名称可能会导致使用它时出现问题。

这是我编写的一个程序的摘录,其中我有一个 partners.json 文件(其中包含不同级别的嵌套对象),我想将 JSON 办公室字段存储在一个对象中。

JSON 文件格式如下:

    [
      {
        "id": 1,
        "urlName": "test-url",
        "organization": "Test Organization",
        "customerLocations": "Global",
        "willWorkRemotely": true,
        "website": "http://www.testurl.com/",
        "services": "This is a test services string",
        "offices": [
          {
            "location": "Random, Earth",
            "address": "Randomness 42, 109 Some St \nRandomville 2000",
            "coordinates": "-33.8934219,151.20404600000006"
          }
        ]
      }, {"id": 2, ... }]

我在我的 JavaScript 代码中导入 JSON:

/* import partner information from the supplied JSON file */
var partners = require('./partners.json');

然后我相应地解析和字符串化所需的“办公室”字段:

/* extract all office locations */
var officeLocations = {};
for (var i = 0; i < partners.length; i++) {
    officeLocations[i] = JSON.parse(JSON.stringify(partners[i].offices));
}

希望这会有所帮助。

【讨论】:

  • 请与我分享示例。我如何在我的代码中做到这一点
  • 如何获取所有 json 数据。所以我应该得到 JSON 对象
  • 这个例子非常适合你想要做的事情:mkyong.com/javascript/how-to-access-json-object-in-javascript
  • 但是我有动态生成的数据,即json文件数据..那么我该如何使用json字符串呢?
  • 我在回答中添加了一个示例
猜你喜欢
  • 2017-10-06
  • 2015-09-30
  • 2016-04-26
  • 2022-01-20
  • 2020-08-14
  • 1970-01-01
  • 2011-10-11
  • 2015-12-20
  • 2016-06-25
相关资源
最近更新 更多