Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript对象中查找属性_Javascript_Jquery_Json_Jsonp - Fatal编程技术网

在javascript对象中查找属性

在javascript对象中查找属性,javascript,jquery,json,jsonp,Javascript,Jquery,Json,Jsonp,我有一个javascript对象,它是从JSONP文件接收的 该对象包含多个“选项”和“结果”,用于在用户单击时调整页面上的html 现在,我能够检查json文件中是否存在HTML字符串(通过json引用插入)。我要做的是获取该字符串,在json文件中找到下一个“结果”或“选项”,然后返回该“选项”或“结果”值,以便我可以使用它来更改html 我该怎么做?我一直在尝试.indexOf方法来查找当前索引,但这并不能真正帮助我找到像“option”这样的特定属性 这是我用来遍历JSONP文件并查找当

我有一个javascript对象,它是从JSONP文件接收的

该对象包含多个“选项”和“结果”,用于在用户单击时调整页面上的html

现在,我能够检查json文件中是否存在HTML字符串(通过json引用插入)。我要做的是获取该字符串,在json文件中找到下一个“结果”或“选项”,然后返回该“选项”或“结果”值,以便我可以使用它来更改html

我该怎么做?我一直在尝试.indexOf方法来查找当前索引,但这并不能真正帮助我找到像“option”这样的特定属性

这是我用来遍历JSONP文件并查找当前字符串是否存在的代码

$.ajax({
    url: "http://www.myurl.com/jsonp.php",
    type: "GET",
    dataType: "jsonp",
    jsonpCallback: "otmjsonp",
    async: false,
    success: function (JSON) {
        $(".result").on("click", function () {
            var currentResult = $(this).text(); //.result is the line of HTML the user has clicked
            for (var playerSelection in JSON) {
                if (JSON.hasOwnProperty(playerSelection)) {
                    if (JSON[playerSelection] === currentResult) {
                        alert("this selection exists in the JSON");
                    }
                }
            }
        })
    }
});
下面是大型JSONP文件的一个非常简单的版本:

otmjsonp({
"situation1" : "Your opponent is trying to tackle you", "playerPrompt1" : "What will you do first?", 

"option1" : "avoid him",

    "result1" : "he tackles you",

        "situation2" : "you were tackled", "playerPrompt2" : "Your opponent begins to slow down",

        "option2" : "chase after him",
            "result2" : "you caught up",
)}
等等等等


即使是模糊的想法/方向也会得到赞赏,因为我完全被卡住了。

如果你重新构建JSON,将选项/结果嵌套在相应的父级中,那么就很容易获得所有可能的选项。您需要将代码更改为:

$.ajax({
url: "http://www.myurl.com/jsonp.php",
type: "GET",
dataType: "jsonp",
jsonpCallback: "otmjsonp",
async: false,
success: function (JSON) {
    $(".result").on("click", function () {
        var currentResult = $(this).text(); //.result is the line of HTML the user has clicked

     if (JSON.hasOwnProperty(playerSelection)) {
      for (var outcome in JSON[playerSelection]) {
       if (JSON[playerselection].hasOwnProperty(outcome)) {
        alert("This is the next outcome " + JSON[playerSelection][outcome]);
       }
      }
     }
   })
  }
});

您可以访问对象属性,如:
Object.property
Object['some property']
。您可以使用
for in
循环来循环对象和大多数数组,如:

var property, value;
for(var i in Object){
  property = i;
  value = Object[i];
}

我建议在进一步研究之前仔细考虑并组织JSON结构。有组织的、逻辑性强的JSON将使Javascript变得更容易。对于这种情况——正如我从描述和示例中所能收集到的——我认为在以后的Javascript中具有逻辑意义并证明有用的JSON结构可能如下所示:

{
“情况”:[
{
“描述”:“你的对手正试图对付你。”,
“提示”:“你会怎么做?”,
“选项”:[
{
“行动”:避开他,
“结果”:“他抓住了你。”
},
{/*另一个选项(操作加结果)*/}
]
},
{/*另一种情况(描述、提示和选项数组)*/}
]
}

我知道这并不能完全解决您的问题,但我认为这将是重新思考您的方法的好地方。

这里的部分问题是您如何将UI与数据初始化相结合。我认为您真正想要做的是将获取数据的JSON请求与点击处理分离开来

$(function() {

  var setupHTML, 
      handleClick,
      updateHTML,
      originalData,
      resultData;

  updateHTML = function(JSON) {
    // Add or activate the html the person is clicking
    $('.result').text()
  };

  handleClick = function(e) {
    var currChoice = $(e.target).text();

    if (resultData === undefined) {
      e.stopPropagation();
      e.preventDefault();
      return;
    }

    for (var ps in resultData) {
      if (resultData.hasOwnProperty(ps) && resultData[ps] === currChoice) {
        resultData = resultData[ps];
        updateHTML(resultData);
      }
    }
  }

  $('.result').on('click', handleClick)


  $.ajax({
    url: "http://www.myurl.com/jsonp.php",
    type: "GET",
    dataType: "jsonp",
    jsonpCallback: "otmjsonp",
    async: false,
    success: function(data) {
      resultData = origData = data;

      // make the UI visible
      setupHTML(JSON);
    }
    });

});

JSONP文件必须是那种格式吗?它看起来是非结构化的,这就是它的困难所在。
currentResult
超出了范围。您在函数内部声明它,这意味着无法从外部访问它。您可能希望将语句
var currentResult
放高一些级别。实际上,当我想到它时,在success函数中定义click事件处理程序也有点奇怪。有什么原因吗?@RaulMartins为了解决这个问题,我已经尽可能简化了JSONP文件——它以前有嵌套的级别,现在所有的东西都在一个级别上。您建议使用什么其他结构?@basilikum复制代码时出错-currentResult应该在范围内(现在已经编辑了)-我正在成功函数中定义click事件处理程序,以便(希望)继续使用ajax调用返回的字符串更改刚刚单击的内容的HTML。我可能可以在成功函数之外完成它,但在我处理这个问题时,它会更容易…@cs_stackX我想建议你将它嵌套。它将简化您的访问。然后你可以直接用JSON[playerSelection]访问它,它会立即返回所有可用的选项/结果。谢谢,这就是我需要的。