Replace null values to empty values in a JSON OBJECT

前端 未结 8 1777
無奈伤痛
無奈伤痛 2020-12-31 06:54

Hi I\'ve got a JSON object provided by an ajax request.

Some of the values inside the json appears as null, but I want an empty String inst

8条回答
  •  清歌不尽
    2020-12-31 07:41

    I recommend you the check the return type of the Ajax because it determines how to remove null.

    If it is string, you can use this:

    data = data.replace(/null/g, '""');  //Stays as string
    

    Or:

    data = JSON.parse(data);    // Turns to a JSON object
    

    If it is json object, then you can use this:

    data = JSON.stringify(data).replace(/null/g, '""');  //Turns to string
    

提交回复
热议问题