【问题标题】:how ll i read the followong json response of a web service?我将如何阅读 Web 服务的以下 json 响应?
【发布时间】:2013-07-24 11:14:59
【问题描述】:

我将如何在 java 脚本中读取以下 json 响应?

这是一个由 javascript 处理的 Web 服务响应,但我不知道如何阅读每个提交的标题和开始日期。 任何帮助表示感谢。

 ([
{
    "submission": {
        "title": "Attended band concert",
        "full": "met with homeroom teacher after concert, very nice, Joker Jr. is         doing well",
        "start_date": "2013-06-18",
        "end_date": null
    }
},
{
    "submission": {
        "title": "she's complaining about",
        "full": "something",
        "start_date": "2013-06-20",
        "end_date": null
    }
}

]);

【问题讨论】:

  • 那不是 JSON。它周围有()

标签: javascript jquery json jsonp


【解决方案1】:

请看一下这个例子,

JSON

{
"news1": "This is a first sample news feed",
"news2": "This is a second sample news feed",
"news3": "This is a third sample news feed"
}

HTML

<ul>
  <li><div id="news1" style="height:100px;width:auto"></div> </li>
  <li><div id="news2" style="height:100px;width:auto"></div></li>
  <li><div id="news3" style="height:100px;width:auto"></div> </li>
</ul>

jQuery

<script type="text/javascript">
 $(document).ready(
  function() {
      $.getJSON('script/news1.js', function(jd) {
   $('#news1').append('<p>' + jd.news1 + '</p>');
   $('#news2').append('<p>' + jd.news2 + '</p>');
   $('#news3').append('<p>' + jd.news3 + '</p>');  
   });  
 });
</script> 

【讨论】:

  • 是的,但这里的 OP 似乎使用了一个 json 对象数组。似乎需要一个 for 循环
【解决方案2】:

如果您以字符串形式获得此答案,则必须在开头和结尾删除 ()。然后,您可以使用 jQuery 函数 $.parseJSON(string) 将字符串解析为 javascript 对象,并将生成的对象分配给变量,这样您就可以像往常一样访问属性。

var object = $.parseJSON(response.substring(1, response.length-2));

要访问第一次提交的标题使用object[0].submission.title,访问第i次提交的标题使用object[i].submission.title

【讨论】:

    【解决方案3】:

    先用substring()去掉()

    使用原生的JSON.parse()函数获取json对象

    然后循环遍历数组。

    var x=xhr.response,
    y=x.substring(1,x.length-2),//or -3
    o=JSON.parse(y);
    for(var a=0;a<o.length;a++){
     var s=o[a].submission;
     console.log(s.title,s.full,s.start_date,s.end_date);
    }
    

    短途

    var x=xhr.response;
    for(var a=0,b;b=JSON.parse(x.substring(1,x.length-2))[a];++a){
     console.log(b.title,b.full,b.start_date,b.end_date);
    }
    

    最快的方式(倒车)

    var x=JSON.parse(xhr.response.substring(1,xhr.response.length-2)),l=x.length;
    while(l--){
     var a=x[l].submission;
     // even if it's reversed l can be used as index for your elements
     console.log(a.title,a.full,a.start_date,a.end_date);
    }
    

    【讨论】:

      【解决方案4】:
      var data=[
      {
          "submission": {
              "title": "Attended band concert",
              "full": "met with homeroom teacher after concert, very nice, Joker Jr. is         doing well",
              "start_date": "2013-06-18",
              "end_date": null
          }
      },
      {
          "submission": {
              "title": "she's complaining about",
              "full": "something",
              "start_date": "2013-06-20",
              "end_date": null
          }
      }]
      
         `alert(data[0].submission.title)`//it gives the title  
              alert(data[0].submission.start_date)//gives date
      

      你也可以放置for循环来读取所有的属性。

         for(var i=0;i<=data.length;i++){
            alert(data[i].submission.title)//gives title
            alert(data[i].submission.start_date)//gives date
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-23
        相关资源
        最近更新 更多