Plurk 服務 .net C# 開發週記(三) - JSON Parsing Exception 的解法

  • 15624
  • 0
  • 2009-10-23

因為受不了 NetServ.Net.Json 這個 JSON Parsing 的 Library
一直發生某些格式有問題 Plurk 訊息沒法被 Parsing
所以手動把 PlurkMessageResponse 及 PlurkMessage
這兩個函式加上了 Try / Catch
若遇到無法 Parsing 的 Plurk 屬性資料, 設定成空值 或 0 ...

因為受不了 NetServ.Net.Json 這個 JSON Parsing 的 Library

一直發生某些格式有問題 Plurk 訊息沒法被 Parsing

所以手動把 PlurkMessageResponse 及 PlurkMessage

這兩個函式加上了 Try / Catch

若遇到無法 Parsing 的 Plurk 屬性資料, 設定成空值 或 0

將有問題的 Plurk 訊息過濾~

        public PlurkMessageResponse(string jsonString)
        {
            JsonObject jsonObject;
 
            try
            {
                using (JsonParser parser = new JsonParser(new StringReader(jsonString), true))
                    jsonObject = parser.ParseObject();
 
                try { this.lang = ((JsonString)jsonObject["lang"]).Value; }
                catch { this.lang = ""; }
 
                try { this.content_raw = ((JsonString)jsonObject["content_raw"]).Value; }
                catch { this.content_raw = ""; }
 
                try { this.user_id = Convert.ToInt32(((JsonNumber)jsonObject["user_id"]).Value); }
                catch { this.user_id = 0; }
 
                try { this.qualifier = ((JsonString)jsonObject["qualifier"]).Value; }
                catch { this.qualifier = ""; }
 
                try { this.plurk_id = Convert.ToInt32(((JsonNumber)jsonObject["plurk_id"]).Value); }
                catch { this.plurk_id = 0; }
 
                try { this.content = ((JsonString)jsonObject["content"]).Value; }
                catch { this.content = ""; }
 
                try { this.id = Convert.ToInt32(((JsonNumber)jsonObject["id"]).Value); }
                catch { this.id = 0; }
 
                try { this.posted = ((JsonString)jsonObject["posted"]).Value; }
                catch { this.posted = ""; }
            }
            catch
            {
                this.lang = "";
                this.content_raw = "";
                this.user_id = 0;
                this.qualifier = "";
                this.plurk_id = 0;
                this.content = "";
                this.id = 0;
                this.posted = "";
            }
        }

 

 

        public PlurkMessage(string jsonString)
        {
            JsonObject jsonObject;
            try
            {
                using (JsonParser parser = new JsonParser(new StringReader(jsonString), true))
                    jsonObject = parser.ParseObject();
 
                try { this.response_count = Convert.ToInt32(((JsonNumber)jsonObject["response_count"]).Value); }
                catch { this.response_count = 0; }
 
                try { this.responses_seen = Convert.ToInt32(((JsonNumber)jsonObject["responses_seen"]).Value); }
                catch { this.responses_seen = 0; }
 
                try { this.qualifier = ((JsonString)jsonObject["qualifier"]).Value; }
                catch { this.qualifier = ""; }
 
                try { this.plurk_id = Convert.ToInt32(((JsonNumber)jsonObject["plurk_id"]).Value); }
                catch { this.plurk_id = 0; }
 
                try { this.is_mute = Convert.ToInt32(((JsonNumber)jsonObject["is_mute"]).Value); }
                catch { this.is_mute = 0; }
 
                try { this.limited_to = ((JsonString)jsonObject["limited_to"]).Value; }
                catch { this.limited_to = ""; }
 
                try { this.no_comments = Convert.ToInt32(((JsonNumber)jsonObject["no_comments"]).Value); }
                catch { this.no_comments = 0; }
 
                try { this.is_unread = Convert.ToInt32(((JsonNumber)jsonObject["is_unread"]).Value); }
                catch { this.is_unread = 0; }
 
                try { this.lang = ((JsonString)jsonObject["lang"]).Value; }
                catch { this.lang = ""; }
 
                try { this.content_raw = ((JsonString)jsonObject["content_raw"]).Value; }
                catch { this.content_raw = ""; }
 
                try { this.user_id = Convert.ToInt32(((JsonNumber)jsonObject["user_id"]).Value); }
                catch { this.user_id = 0; }
 
                try { this.id = Convert.ToInt32(((JsonNumber)jsonObject["id"]).Value); }
                catch { this.id = 0; }
 
                try { this.content = ((JsonString)jsonObject["content"]).Value; }
                catch { this.content = ""; }
 
                try { this.source = Convert.ToInt32(((JsonNumber)jsonObject["source"]).Value); }
                catch { this.source = 0; }
 
                try { this.posted = ((JsonString)jsonObject["posted"]).Value; }
                catch { this.posted = ""; }
 
                try { this.owner_id = Convert.ToInt32(((JsonNumber)jsonObject["owner_id"]).Value); }
                catch { this.owner_id = 0; }
            }
            catch
            {
                this.response_count = 0;
                this.responses_seen = 0;
                this.qualifier = "";
                this.plurk_id = 0;
                this.is_mute = 0;
                this.limited_to = "";
                this.no_comments = 0;
                this.is_unread = 0;
                this.lang = "";
                this.content_raw = "";
                this.user_id = 0;
                this.id = 0;
                this.content = "";
                this.source = 0;
                this.posted = "";
                this.owner_id = 0;
            }
            this.responses = new PlurkMessageResponses();
        }