猿问

我想根据它的节点将以下json字符串显示到html表中。我无法做到。因为我是json的新手

Json 字符串如下所示:-


{

      d: {"lstname":

                  {

                     "Fname":"test",

                     "LName":"data"

                  },


            "lstAddress": 

                 {

                     "address1":"abc",

                     "address2":"def",

                     "pincode":"0",

                     "Area":"Bhandup"

                 }

       }

 }

代码


<body>

    <form id="form1" runat="server">

        <%--<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />--%>

       <input type="button" name="btn1" id="Getdata" value="Getdata" />

        <div id="name">


        </div>

        <div id="Addess">

        </div>

    </form>

</body>

lstname 数据应显示在名称 div 中,lstaddress 数据应显示在地址 div 中


MMTTMM
浏览 73回答 1
1回答

慕森王

我已经修复了一些JSON - 我已经在'd:'属性中添加了“”。所以这里的工作版本:在JSFiddle中var str = `{&nbsp; "d": {"lstname":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"Fname":"test",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"LName":"data"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; "lstAddress":&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"address1":"abc",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"address2":"def",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"pincode":"0",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"Area":"Bhandup"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}}`;&nbsp;var obj = JSON.parse(str);&nbsp;$(function(){&nbsp; &nbsp; $('#name').text(obj.d.lstname.Fname + ' ' + obj.d.lstname.LName)&nbsp; $('#Addess').text(obj.d.lstAddress.address1 + ' ' + obj.d.lstAddress.address2 + ' ' + obj.d.lstAddress.pincode + ' ' + obj.d.lstAddress.Area)&nbsp;});
随时随地看视频慕课网APP
我要回答