1

大家好,我正在连接这样的JSON字符串:

      var addresses = "[";

         addresses += '{"AddressID":' + adressid + ',"EmailID":' + $('#txtemailData').val() + ',"Hno":' + $('#txthno').val() + ',"StreetName":' + $('#txtstreetname').val() + ',"City":' + $('#txtcity').val() + ',"StateID":' + $('#ddlState').val() + ',"CountryID":' + $('#ddlcountry').val() + ',"Zip":' + $('#txtzip').val() + ',"PhoneNumber":' + $('#txtphonenumber').val() + ',"Fax":' + $('#txtfax').val() + ',"AddressName:' + $('#txtaddresstype').val() + '"},';

对象看起来像这样:

[{
   "AddressID":2,
   "EmailID":akanilkumar443@gmail.com,
   "Hno":Hyderabad,
   "StreetName":Gachibowli,
   "City":Hyderabad,
   "StateID":1,
   "CountryID":1,
   "Zip":040,
   "PhoneNumber":8341516166,
   "Fax":23123131,
   "AddressName:Store Address"},
 { 
   "AddressID":3,
   "EmailID":akanilkumar443@gmail.com,
   "Hno":aSAs,
   "StreetName":asdasdad,
   "City":asdasda,
   "StateID":1,
   "CountryID":1,
   "Zip":asdasda,
   "PhoneNumber":asdasda,
   "Fax":asdasda,
"AddressName:Store Type"
}]

如何根据它的 id 更新 json 对象的这个特定值?

假设我想更改我的对象 where 的一些值AddressID=2。例如,我想更改EmailID,StreetnameJSON对象 where AddressID=2。如何使用 jQuery 做到这一点?

我正在尝试这样,但它没有进入循环,任何人都可以在这里帮助我吗?

    function EditAddress(addressid) {
    alert(addressid);
    alert(addresses);
    var addressobject =JSON.parse(addresses.substring(0, addresses.length - 1) + ']');
    jQuery.each(addressobject, function (i, val) {
        alert(val.AddressID);
        if (val.AddressID == addressid) 
        {
            //update logic
        }
    });
}
4

3 回答 3

3

您可以arr使用该$.each()函数循环遍历数组,然后搜索id属性值在哪里2。如果找到,则更新对象中所需的属性,obj然后像这样跳出循环:

var arr = [
    {"id": 1, "name": "Apple"  , "isVisible": false},
    {"id": 2, "name": "Orange", "isVisible": false},
    {"id": 3, "name": "Banana", "isVisible": false}
]

$.each( arr, function( i, obj ) {
  if(obj.id === 2){
    console.log("Current " + obj.id + " = " + obj.isVisible);
    obj.isVisible = true;
    console.log("Changed " + obj.id + " = " + obj.isVisible);
    return false; // Loop will stop running after this
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7/jquery.min.js"></script>

于 2012-11-19T11:55:57.913 回答
0

首先不要手动创建字符串。使用内置功能要强大得多,因此请执行以下操作:

var addressesAsArray = [],
    addressAsObject = {}
    address;
//assuming some loop or other
address = {
              "AddressID": adressid,
              "EmailID":$('#txtemailData').val(),
              "Hno":$('#txthno').val(),
              "StreetName": $('#txtstreetname').val(),
              "City": $('#txtcity').val(),
              "StateID": $('#ddlState').val(),
              "CountryID": $('#ddlcountry').val(),
              "Zip": $('#txtzip').val(),
              "PhoneNumber": $('#txtphonenumber').val(),
              "Fax": $('#txtfax').val(),
              "AddressName": $('#txtaddresstype').val()
          };
addressesAsArray.push(address);
addressAsObject[address.AddressID] = address;    

如果您需要查找具有给定 ID 的地址,该方法将取决于您是在addressesAsArray 中还是在addressesAsObject 中查找。后者直截了当

address = addressesAsObject[addressIDBeingSought];

在数组情况下,您可以简单地循环

for(i = 0, len = addressesAsArray.length;i<len; i += 1){
   if(addressesAsArray[i].AddressID === addressIDBeingSought) {
       address = addressesAsArray[i];
       break;
   }
}

完成更新后,您可以通过以下方式将其作为 JSON 获取

json = JSON.stringify(adresses);
于 2012-11-19T11:28:58.970 回答
-1

使用 linq.js javascript 库或 jquery 插件:http ://linqjs.codeplex.com/

    <!DOCTYPE>
    <html>
    <head>
        <script type="text/javascript" src="linq.js"></script>
    </head>
    <body>
    <script>  
    var array = [{
       AddressID:2,
       EmailID:'akanilkumar443@gmail.com',
       Hno:'Hyderabad'
       },
     { 
       AddressID:3,
       EmailID:'akanilkumar443@gmail.com',
       Hno:'aSAs'
    }];
    Enumerable.From(array).Where("$.AddressID == 3").ToArray()[0].Hno= 'ololo'; 
// or this: 
// Enumerable.From(array).Where(function(x){return x.AddressID == 3}).ToArray()[0].Hno= 'ololo'; 
    alert(array[1].Hno)  
    </script>
    </body>
    </html>
于 2012-11-19T11:13:59.613 回答