这是更新Json对象数组值的方法吗?

时间:2016-04-14 09:24:23

标签: json node.js

  

我有一个类似下面的JSON:

[ { message: '',
    fileSize: '14534541',
    id: '7128',
    readBy: '2016-04-13 03:58:18',
    userID: '337',
    dateTime: '2016-04-12 06:07:12',
    fileName: 'ecdba036-9b0b-4c40-b64d-c82ce8765f14.mp4',
    fileTicketID: 'ecdba036-9b0b-4c40-b64d-c82ce8765f14' },
  { message: '',
    fileSize: '14534541',
    id: '7133',
    readBy: '2016-04-13 03:58:18',
    userID: '337',
    dateTime: '2016-04-12 06:47:40',
    fileName: 'c8275638-72f8-4a9b-86d7-803fa9b5723c.mp4',
    fileTicketID: 'c8275638-72f8-4a9b-86d7-803fa9b5723c' },
  { message: '',
    fileSize: '81358',
    id: '7243',
    readBy: '2016-04-13 03:58:18',
    userID: '337',
    dateTime: '2016-04-13 01:41:20',
    fileName: '6f56469a-4e05-431e-b340-c999834f0ce6.png',
    fileTicketID: '6f56469a-4e05-431e-b340-c999834f0ce6' },
  { message: '',
    fileSize: '489102',
    id: '7244',
    readBy: '2016-04-13 03:58:18',
    userID: '337',
    dateTime: '2016-04-13 01:44:21',
    fileName: 'c89999ad-e883-46bb-ad4b-5d6f4b417288.png',
    fileTicketID: 'c89999ad-e883-46bb-ad4b-5d6f4b417288' },
  { message: '',
    fileSize: '6366313',
    id: '7248',
    readBy: '2016-04-13 03:58:18',
    userID: '337',
    dateTime: '2016-04-13 01:59:44',
    fileName: 'd5fe7f63-b03b-406c-8291-1fe2140a4fc5.png',
    fileTicketID: 'd5fe7f63-b03b-406c-8291-1fe2140a4fc5' },
  { message: '',
    fileSize: '14534541',
    id: '7262',
    readBy: '2016-04-13 03:58:18',
    userID: '337',
    dateTime: '2016-04-13 03:19:40',
    fileName: '44ba3f90-333a-47d4-9b14-d462a61b8af7.mp4',
    fileTicketID: '44ba3f90-333a-47d4-9b14-d462a61b8af7' },
  { message: '',
    fileSize: '44058',
    id: '7274',
    readBy: '2016-04-13 04:03:26',
    userID: '337',
    dateTime: '2016-04-13 04:02:19',
    fileName: '138edca1-7c29-4184-82e7-dc0a7549ebbe.png',
    fileTicketID: '138edca1-7c29-4184-82e7-dc0a7549ebbe' },
  { message: '',
    fileSize: '655803',
    id: '7275',
    readBy: '2016-04-13 04:03:26',
    userID: '337',
    dateTime: '2016-04-13 04:02:32',
    fileName: 'a7362d98-1746-4218-94a3-b5189de704ea.png',
    fileTicketID: 'a7362d98-1746-4218-94a3-b5189de704ea' },
  { message: '',
    fileSize: '108468',
    id: '7307',
    readBy: '',
    userID: '337',
    dateTime: '2016-04-13 04:59:38',
    fileName: '1e42b6cd-b628-4439-b55c-9c485a2ef0f2.png',
    fileTicketID: '1e42b6cd-b628-4439-b55c-9c485a2ef0f2' },
  { message: '',
   fileSize
    id: '7309',
    readBy: '',
    userID: '337',
    dateTime: '2016-04-13 05:00:22',
    fileName: '0b6d601b-8a0f-4877-819b-e2417ed8c77b.mp4',
    fileTicketID: '0b6d601b-8a0f-4877-819b-e2417ed8c77b' } ]
  

我想要的是我在循环中有代码

for(var i=0;i<n.length;i++)
{

if(condition)
{
then update the value of fileSize to 0
then josn array variable shown like
{ message: '',
        fileSize: 0,
        id: '7128',
        readBy: '2016-04-13 03:58:18',
        userID: '337',
        dateTime: '2016-04-12 06:07:12',
        fileName: 'ecdba036-9b0b-4c40-b64d-c82ce8765f14.mp4',
        fileTicketID: 'ecdba036-9b0b-4c40-b64d-c82ce8765f14' },
}

}


}
  
    

如何更改json中的变量值。我想检查数据库中的文件的值,如果根据该文件名为零     然后我需要更新filesize 0

  

2 个答案:

答案 0 :(得分:0)

    Use below code 

    <pre>
    $jsonS = '[ { "message": "",   "fileSize": 14534541,    "id": 7128,    "readBy": "2016-04-13 03:58:18",    "userID": 337,    "dateTime": "2016-04-12 06:07:12",    "fileName": "ecdba036-9b0b-4c40-b64d-c82ce8765f14.mp4",    "fileTicketID": "ecdba036-9b0b-4c40-b64d-c82ce8765f14" },{ "message": "",   "fileSize": 0,    "id": 7128,    "readBy": "2016-04-13 03:58:18",    "userID": 337,    "dateTime": "2016-04-12 06:07:12",    "fileName": "ecdba036-9b0b-4c40-b64d-c82ce8765f14.mp4",    "fileTicketID": "ecdba036-9b0b-4c40-b64d-c82ce8765f14" } ]';
$jsonArray = json_decode($jsonS);
foreach($jsonArray as $k=>$json)
{
    if(check the value of file in database if that is zero according to that filename)
    {
        $jsonArray[$k]->fileSize=0;
    }

}
print_r($jsonArray);

    </pre>

答案 1 :(得分:0)

如果我理解正确,您可以使用async库执行以下操作:

var array = [
{ message: '',
fileSize: '14534541',
id: '7128',
readBy: '2016-04-13 03:58:18',
userID: '337',
dateTime: '2016-04-12 06:07:12',
fileName: 'ecdba036-9b0b-4c40-b64d-c82ce8765f14.mp4',
fileTicketID: 'ecdba036-9b0b-4c40-b64d-c82ce8765f14' }, ... ];

async.eachSeries(array, function iteratee(item, callback) {
    db.query("SELECT * FROM ... WHERE ID=?", item.id, function(err, result) {
        if (result[0].fileSize == 0) {
            item.fileSize = 0;
        }
        callback();
    }
}, function done() {
    console.log(array);
};