如何使用JavaScript / AngularJS解析此JSON文件?

时间:2017-03-23 10:29:12

标签: javascript angularjs json

我正在尝试解析下面的json。如果我尝试获取值,请参阅 以下代码显示未定义。

有什么办法可以解析json中哪个键作为空格?

 {
        "Trasport Fee":{
        "KARNATAKA":{
        "ALL_DISTRICT":{
        "ALL_PLACES":{
        "amount":4000
        }
        }
        },
        "ANDRA":{
        "ALL_DISTRICT":{
        "ALL_PLACES​":{
        "amount":100
        }
        }
        },
        "MP":{
        "ALL_DISTRICT":{
        "ALL_PLACES​":{
        "amount":600
        }
        }
        },
        "MAHARASHTRA":{
        "ALL_DISTRICT":{
        "ALL_PLACES​":{
        "amount":600
        }
        }
        }
        }
    }
      var promise=feeService.getTraspotaionFee();
        promise.then(function(data){
            $scope.charge=data;
            console.log($scope.charge.data["Trasport Fee"]["KARNATAKA"]["ALL_DISTRICT"]["amount"]);

        });

5 个答案:

答案 0 :(得分:0)

{
        "Trasport Fee":{
        "KARNATAKA":{
        "ALL_DISTRICT":{
        "ALL_PLACES":{
        "amount":4000
        }
        }
        },
        "ANDRA":{
        "ALL_DISTRICT":{
        "ALL_PLACES​":{
        "amount":100
        }
        }
        },
        "MP":{
        "ALL_DISTRICT":{
        "ALL_PLACES​":{
        "amount":600
        }
        }
        },
        "MAHARASHTRA":{
        "ALL_DISTRICT":{
        "ALL_PLACES​":{
        "amount":600
        }
        }
        }
        }

      var promise=feeService.getTraspotaionFee();
        promise.then(function(data){
            $scope.charge=data;
            console.log($scope.charge.data["Trasport Fee"]["KARNATAKA"]["ALL_DISTRICT"]["ALL_PLACES"]["amount"]);

        });

你的钥匙是运输费而不是转账。你在金额之前错过了ALL_PLACES

答案 1 :(得分:0)

您的JSON无效,最后缺少大括号。 使用JSON验证器,这就是我在“JSON”数据出现问题时先做的事情。

https://jsonformatter.curiousconcept.com/< - 漂亮的验证器

答案 2 :(得分:0)

    var promise=feeService.getTraspotaionFee();
        promise.then(function(data){
            $scope.charge=data;

//changes here
            console.log($scope.charge.data[Object.keys($scope.charge.data)[0]]["KARNATAKA"]["ALL_DISTRICT"]["ALL_PLACES"]["amount"]);

        });

更改上述代码并使用

答案 3 :(得分:0)

我认为应该是:

console.log($scope.charge.data["Trasport Fee"]["KARNATAKA"]["ALL_DISTRICT"]["ALL_PLACES"]["amount"]);
                                                                           ^^^^^^^^^^^^^^

我还修复了你的JSON(缺少括号):

{
   "Trasport Fee":{
      "KARNATAKA":{
         "ALL_DISTRICT":{
            "ALL_PLACES":{
               "amount":4000
            }
         }
      },
      "ANDRA":{
         "ALL_DISTRICT":{
            "ALL_PLACES​":{
               "amount":100
            }
         }
      },
      "MP":{
         "ALL_DISTRICT":{
            "ALL_PLACES​":{
               "amount":600
            }
         }
      },
      "MAHARASHTRA":{
         "ALL_DISTRICT":{
            "ALL_PLACES​":{
               "amount":600
            }
         }
      }
   }
}

答案 4 :(得分:0)

观察:

  • 问题不在于密钥中的空格。您只能以正确的方式访问该值。

  • 您未定义,因为您正在尝试访问amount对象内的ALL_DISTRICT属性,该对象不可用。

试试这个:

$scope.charge.data["Trasport Fee"]["KARNATAKA"]["ALL_DISTRICT"]["ALL_PLACES"]["amount"]

<强>样本

var data = {
	"Trasport Fee": {
		"KARNATAKA": {
			"ALL_DISTRICT": {
				"ALL_PLACES": {
					"amount": 4000
				}
			}
		},
		"ANDRA": {
			"ALL_DISTRICT": {
				"ALL_PLACES": {
					"amount": 100
				}
			}
		},
		"MP": {
			"ALL_DISTRICT": {
				"ALL_PLACES": {
					"amount": 600
				}
			}
		},
		"MAHARASHTRA": {
			"ALL_DISTRICT": {
				"ALL_PLACES": {
					"amount": 600
				}
			}
		}
	}
};

console.log(data["Trasport Fee"]["KARNATAKA"]["ALL_DISTRICT"]["ALL_PLACES"]["amount"]);