weixin_33690963 2017-07-11 13:11 采纳率: 0%
浏览 22

从ajax响应中读取[重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/4343596/how-can-i-parse-a-json-file-with-php" dir="ltr">How can I parse a JSON file with PHP? [duplicate]</a>
                            <span class="question-originals-answer-count">
                                (16 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2017-07-11 13:13:48Z" class="relativetime">2 years ago</span>.</div>
        </div>
    </aside>

I receive this as a response from an ajax call. My question is how can I get the values of the object?

{"countries":[{"country_id":730,"country":"El Salvador"},{"country_id":756,"country":"Guatemala"},{"country_id":767,"country":"Indonesia"}]}

my ajax call is this:

$.ajax({
    type: "POST",
    dataType:"json",
    url:getCountriesPackages,
    data: "transferUrl=countries?service_id="+serviceId,
    success: function(data) {

        $.each(data, function(k, v) {
          console.log(k);
        });
    }
});

thank you

</div>
  • 写回答

2条回答 默认 最新

  • 零零乙 2017-07-11 13:13
    关注

    You already have a JSON, you can traverse it using $.each():

    var obj = {
      "countries": [{
        "country_id": 730,
        "country": "El Salvador"
      }, {
        "country_id": 756,
        "country": "Guatemala"
      }, {
        "country_id": 767,
        "country": "Indonesia"
      }]
    };
    
    $.each(obj, function(k, v) {
      $.each(v, function(kk, kv) {
        console.log("Country ID: " + kv.country_id);
        console.log("Country: " + kv.country);
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)