将JSON数据分配给变量或常量并进行字符串比较 - AngularJS

时间:2018-03-07 18:15:01

标签: angularjs json

我是AngularJS的新手,并开始使用测试咖啡馆进行网络单元测试。我的问题在于检查元素我有一组JSON数据,这是在我在文本字段中传递几个文本并单击搜索时生成的。我需要将此JSON数据转换为变量或常量以进行字符串比较。 JSON数据如下:

value = {
    "CVLocation": "",
    "PostalCode": "",
    "DistanceInKms": "",
    "CVJobTitles": "java",
    "CVSkills": "",
    "CVIndustries": "",
    "CVLicenses": "",
    "CVWhiteBoard": "",
    "CVCustomQuery": "",
    "CVFilterDataList": [],
    "JobTitlesSearchedWithIn": 2,
    "SkillsSearchedWithIn": .......
}

有人可以帮助我如何与这些JSON数据进行字符串比较吗?

screenshot of my web page

如何将Element博客中生成的JSON数据提供给我的代码?

2 个答案:

答案 0 :(得分:0)

使用var myJSONString = JSON.stringify(value);将您的JSON转换为字符串进行比较,例如value.CVLocation

如果您想比较JSON的各个属性,只需访问它们: - value.PostalCodefor ($i=0; $i < count($firstNames); $i++){ $description = new Description(); $description->setName($firstNames[$i], $lastNames[$i]); $description->setDescription($descriptions[$i]); echo $description->getDescription(); }

答案 1 :(得分:0)

&#13;
&#13;
docker-cmd: ["R", "-e", "shiny::runApp('/srv/shiny-server/')"]
&#13;
&#13;
&#13;

我们可以像这样遍历对象,并通过像这样的对象的键来比较它的值。 希望有所帮助。

更新的答案:
因为您要检索输入的属性

object={
    "CVLocation": "",
    "PostalCode": "",
    "DistanceInKms": "",
    "CVJobTitles": "java",
    "CVSkills": "",
    "CVIndustries": "",
    "CVLicenses": "",
    "CVWhiteBoard": "",
    "CVCustomQuery": "",
    "CVFilterDataList": [],
    "JobTitlesSearchedWithIn": 2,
    "SkillsSearchedWithIn": ""
}

for(obj in object){
  if(object[obj]==="java")
  console.log(obj)
 }

但是在你的情况下,ID现在还不知道,因为图像中看不到任何已知的内容。因此检查输入元素并右键单击元素代码以获取如下所示的选择器,并获取JS文件中的属性值并应用上面给出的代码。

enter image description here

//Using Vanilla JS
document.getElementById("<some-id>").getAttribute("value");

//Using Jquery
$('#some_id').attr('value')

并使用