在JSON stringify数组中查找值

时间:2015-10-20 23:40:50

标签: javascript arrays json associative-array

我想在JSON stringify数组中找到一个值

manhattan()

我想要获得此值的想法是知道我添加的内容是否尚未添加

3 个答案:

答案 0 :(得分:1)

....

var jsonArray = [{"id":"432","temperature":"1","humidity":"1",.....

var isPresent = false;
$.each(jsonArray, function(i,v){
    if(jsonArray[i].id == newObject.id){
        isPresent = true;
    }
});
if(!isPresent){
    jsonArray.push(newObject);
}

....

答案 1 :(得分:1)

// stringified JSON
stringifiedJson = '[{"id":"432","temperature":"1","humidity":"1","createat":"0000-00-00 00:00:00"},{"id":"433","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:49:57"},{"id":"434","temperature":"22.40","humidity":"48","createat":"2015-10-11 19:52:02"},{"id":"435","temperature":"22.40","humidity":"48","createat":"2015-10-11 19:55:26"},{"id":"436","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:58:50"},{"id":"437","temperature":"22.00","humidity":"48","createat":"2015-10-11 20:02:14"},{"id":"438","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:23:15"},{"id":"439","temperature":"22.50","humidity":"50","createat":"2015-10-11 21:24:37"},{"id":"440","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:26:17"},{"id":"441","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:26:41"}]';

// parse the stringified JSON into a JavaScript object
parsedJson = JSON.parse(stringifiedJson);

// the object in the array you want to check
number = 0

// check if the property exists
if(typeof parsedJson[number].humidity !== 'undefined') {
  // set the property
  parsedJson.humidity = 1;
}

// stringify your object again
stringifiedJson = JSON.stringify(parsedJson);

编辑:这是一个propertyExists函数:

var propertyExists = function(stringifiedJson, id, property) {
  // parse the stringified JSON into a JavaScript object
  parsedJson = JSON.parse(stringifiedJson);
  // check if the property exists for a given ID
  for(var i = 0; i < parsedJson.length; i += 1) {
    if(parseInt(parsedJson[i].id) === parseInt(id)) {
      return (typeof parsedJson[i][property] !== 'undefined')
    }
  }
  return false;
}

// stringified json
var stringifiedJson = '[{"id":"432","temperature":"1","humidity":"1","createat":"0000-00-00 00:00:00"},{"id":"433","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:49:57"},{"id":"434","temperature":"22.40","humidity":"48","createat":"2015-10-11 19:52:02"},{"id":"435","temperature":"22.40","humidity":"48","createat":"2015-10-11 19:55:26"},{"id":"436","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:58:50"},{"id":"437","temperature":"22.00","humidity":"48","createat":"2015-10-11 20:02:14"},{"id":"438","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:23:15"},{"id":"439","temperature":"22.50","humidity":"50","createat":"2015-10-11 21:24:37"},{"id":"440","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:26:17"},{"id":"441","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:26:41"}]';

console.log(propertyExists(stringifiedJson, 432, 'humidity'));

答案 2 :(得分:0)

  

我想在JSON stringify数组中找到一个值

您没有说出您想要找到的价值。您发布的是Array文字,而不是JSON。如果要迭代数组以确定它是否包含具有特定值的特定属性的对象,则可以使用Array.prototype.some

function hasPropValue(array, prop, value) {
  return array.some(function(obj) {
    return obj.hasOwnProperty(prop) && obj[prop] === value;
  }
}

某些将仅对存在的成员进行迭代,并在回调返回 true 时立即停止,否则返回false。如果你确实有一个有效的JSON字符串值,你可以这样做:

hasPropValue(JSON.parse(jsonText), 'id', '432');

var jsonText = '[{"id":"432","temperature":"1","humidity":"1","createat":"0000-00-00 00:00:00"},{"id":"433","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:49:57"},{"id":"434","temperature":"22.40","humidity":"48","createat":"2015-10-11 19:52:02"},{"id":"435","temperature":"22.40","humidity":"48","createat":"2015-10-11 19:55:26"},{"id":"436","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:58:50"},{"id":"437","temperature":"22.00","humidity":"48","createat":"2015-10-11 20:02:14"},{"id":"438","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:23:15"},{"id":"439","temperature":"22.50","humidity":"50","createat":"2015-10-11 21:24:37"},{"id":"440","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:26:17"},{"id":"441","temperature":"22.50","humidity":"51","createat":"2015-10-11 21:26:41"}]'

function hasPropValue(array, prop, value) {
  return array.some(function(obj) {
    return obj.hasOwnProperty(prop) && obj[prop] === value;
  });
}

document.write(hasPropValue(JSON.parse(jsonText), 'id', '432'));