如何根据用户的答案浏览Json文件

时间:2017-04-18 14:41:47

标签: javascript jquery json

我正在努力开发一款游戏"。您可以使用不同的路径,例如您可以决定下一步的故事情节,并且我已经创建了一个Json,如下所示。

现在,用户收到一个问题和两个选项,答案将影响下一个问题和答案。就像一个树形结构。

我无法理解如何前往这样的Json儿童。 到目前为止我有这个:

RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.rlayout);
RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
Button tv1 = new Button(this);
tv1.setText("Hello");
tv1.setLayoutParams(lprams);
tv1.setId(1);
rLayout.addView(tv1);

// second Button
RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
Button tv2 = new Button(this);
tv1.setText("Hello2");
newParams.addRule(RelativeLayout.RIGHT_OF, 1);
tv2.setLayoutParams(newParams);
tv2.setId(2);
rLayout.addView(tv2);
{
  "label": null,
  "question": "Vamos começar a noite??",
  "children": [
    {
      "label": "Sim",
      "question": "Jantamos onde?",
      "children": [
        {
          "label": "Casa",
          "question": "Qual?",
          "children": [
            {
              "label": "Minha",
              "question": null,
              "children": null
            },
            {
              "label": "Tua",
              "question": null,
              "children": null
            }
          ]
        },
        {
          "label": "Restaurante",
          "question": "Qual?",
          "children": [
            {
              "label": "Temudus",
              "question": null,
              "children": null
            },
            {
              "label": "Pancinhas",
              "question": null,
              "children": null
            }
          ]
        }
      ]
    },
    {
      "label": "Não",
      "question": "Tens a certeza?",
      "children": [
        {
          "label": "Sim",
          "question": "Vais para casa?",
          "children": [
            {
              "label": "Em princípio sim",
              "question": null,
              "children": null
            },
            {
              "label": "Talvez não",
              "question": null,
              "children": null
            }
          ]
        },
        {
          "label": "Não",
          "question": "Vamos à praça?",
          "children": [
            {
              "label": "Siga",
              "question": null,
              "children": null
            },
            {
              "label": "Nem pensar",
              "question": null,
              "children": null
            }
          ]
        }
      ]
    }
  ]
}

谢谢

1 个答案:

答案 0 :(得分:0)

您可以将当前节点存储在变量中吗?

//GLOBALS:
var data=null;
var node=null;
document).ready(function() {

$.ajax({
  dataType: "json",
  url: "..." ,
    }).done(function ( d) {
        node=data=d;
        showNewNode();
        init();
   });
});
function showNewNode(){
    //these are the first question and answers. And this works!!
    $('#titulo').html(node.question);
    $('#0').html(node.children[0].label);
    $('#1').html(node.children[1].label);
}
    //when the user clicks on one of the options
function init(){
    $('.all_options').bind('click', function () {
        var op = event.target.id;
        console.log(op);

        if(op == '0'){
           node=node.children[0];
        }else{
           node=node.children[1];
        }
       showNewNode();
    });
}