从json string属性中删除撇号

时间:2011-01-12 15:38:20

标签: c# json serialization json.net

您好我尝试使用JSON.NET反序列化此字符串。

字符串看起来像这样:

{
    "mishelka.sk": {
        "idUser": 15289422,
        "nick": "MiShelKa.Sk",
        "photo": "1",
        "sex": 2,
        "photoAlbums": 4,
        "videoAlbums": 3,
        "sefNick": "mishelka.sk",
        "profilPercent": 78,
        "emphasis": false,
        "age": 14,
        "isBlocked": false,
        "PHOTO": {
            "normal": "http://u.aimg.sk/fotky/1528/94/n_15289422.jpg?v=4",
            "medium": "http://u.aimg.sk/fotky/1528/94/m_15289422.jpg?v=4",
            "24x24": "http://u.aimg.sk/fotky/1528/94/s_15289422.jpg?v=4"
        },
        "PLUS": {
            "active": false,
            "activeTo": "0000-00-00"
        },
        "LOCATION": {
            "idRegion": "10",
            "regionName": "zahraničie",
            "idCity": "182",
            "cityName": "Zahraničie - GB"
        },
        "STATUS": {
            "isLoged": false,
            "isChating": false,
            "idChat": 0,
            "roomName": "",
            "lastLogin": 1294767618
        },
        "PROJECT_STATUS": {
            "photoAlbums": 4,
            "photoAlbumsFavs": 1,
            "videoAlbums": 3,
            "videoAlbumsFavs": 33,
            "videoAlbumsExts": 0,
            "blogPosts": 0,
            "emailNew": 1,
            "postaNew": 0,
            "clubInvitations": 0,
            "dashboardItems": 54
        },
        "STATUS_MESSAGE": {
            "statusMessage": "Jaj henka zabila si dneska hah",
            "addTime": "1294678134"
        },
        "isFriend": false,
        "isIamFriend": false
    },
    "mishel431": {
        "idUser": 18327105,
        "nick": "mishel431",
        "photo": "1",
        "sex": 2,
        "photoAlbums": 1,
        "videoAlbums": 0,
        "sefNick": "mishel431",
        "profilPercent": 98,
        "emphasis": false,
        "age": 15,
        "isBlocked": false,
        "PHOTO": {
            "normal": "http://u.aimg.sk/fotky/1832/71/n_18327105.jpg?v=5",
            "medium": "http://u.aimg.sk/fotky/1832/71/m_18327105.jpg?v=5",
            "24x24": "http://u.aimg.sk/fotky/1832/71/s_18327105.jpg?v=5"
        },
        "PLUS": {
            "active": false,
            "activeTo": "0000-00-00"
        },
        "LOCATION": {
            "idRegion": "1",
            "regionName": "Banskobystrický kraj",
            "idCity": "70",
            "cityName": "Lučenec"
        },
        "STATUS": {
            "isLoged": false,
            "isChating": false,
            "idChat": 0,
            "roomName": "",
            "lastLogin": 1294760188
        },
        "PROJECT_STATUS": {
            "photoAlbums": 1,
            "photoAlbumsFavs": 0,
            "videoAlbums": 0,
            "videoAlbumsFavs": 0,
            "videoAlbumsExts": 0,
            "blogPosts": 0,
            "emailNew": 1,
            "postaNew": 0,
            "clubInvitations": 0,
            "dashboardItems": 14
        },
        "STATUS_MESSAGE": {
            "statusMessage": " I hate "apple" lalala:(",
            "addTime": "1293656085"
        },
        "isFriend": false,
        "isIamFriend": false
    }

}

我尝试将此字符串反序列化为dic,一切正常,但我有一个问题。

就是这样:

  "STATUS_MESSAGE": {
            "statusMessage": " I hate "apple" lalala:(",
            "addTime": "1293656085"
        },

statusMessage属性可以输入撇号,这会导致无效的异常json字符串。我希望在所有json字符串中优雅地从字符串属性statusMessage中删除瑕疵。

任何建议。使用regExp。我在这个领域并不强大。

此部分在此类中反序列化:

  [Export]
public class StatusMessage : INotifyPropertyChanged
{
    #region Private Fields

    private string _message;

    #endregion

    #region Properties

    [JsonProperty("statusMessage")]
    public string Message
    {
        get { return _message; }
        set
        {
            if (_message != value)
            {
                _message = value;
                NotifyPropertyChanged("Message");
            }
        }
    }

    [JsonProperty("addTime")]
    public string AddTime { get; set; }

    #endregion

    #region Implementation of INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    #region Public methods

    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion
}

1 个答案:

答案 0 :(得分:2)

Json字符串的生成器应使用反斜杠转义引号 查看Json网站上的规范,这样您就不必担心错误形成的Json字符串