2016-07-31 24 views
0

私はオンラインモードでアプリケーションを動作させる/切断し、ASPNET開発者のための書籍HTML5プログラミングの例で動作します。ASPNET LocalStorage Json逆シリアル化

私はASPNETテクノロジMVC5を使用します。

私は私のコントローラ内のデータをデシリアライズするとき、私はエラーを持っている:

public JsonResult SaveResults() 
    { 
     string jsonData = string.Empty; 
     using (StreamReader sr = new StreamReader(Request.InputStream)) 
     { 
      jsonData = sr.ReadToEnd(); 
     } 
     Dictionary<string, string> data = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonData); 

     SurveyDbEntities db = new SurveyDbEntities(); 

     User usr = new User(); 
     usr.FirstName = data["FirstName"]; 
     usr.LastName = data["LastName"]; 
     usr.Email = data["Email"]; 
     db.Users.AddObject(usr); 
     db.SaveChanges(); 

     string userEmail = data["Email"]; 
     int usrId = (from item in db.Users 
         where item.Email == userEmail 
         select item.UserID).SingleOrDefault(); 

     data.Remove("FirstName"); 
     data.Remove("LastName"); 
     data.Remove("Email"); 

     foreach (string str in data.Keys) 
     { 
      int choiceId = int.Parse(str); 
      int questionId = int.Parse(data[str]); 
      Result result = new Result(); 
      result.QuestionID = questionId; 
      result.ChoiceID = choiceId; 
      result.UserID = usrId; 
      db.Results.AddObject(result); 
     } 
     db.SaveChanges(); 
     return Json("success"); 
    } 

この行はエラー

Dictionary<string, string> data = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonData); 

マイjsonData(引用問題?)を示しています。

{"Email":"[email protected]","FirstName":"test","LastName":"test","container":"<div data-questions-questionid="1" class="paddedDiv">Which programming language do you use?<br><input type="checkbox" data-choices-questionid="1" data-choices-choiceid="1"><span>C#</span><br><input type="checkbox" data-choices-questionid="1" data-choices-choiceid="2"><span>VB.NET</span><br><input type="checkbox" data-choices-questionid="1" data-choices-choiceid="3"><span>PHP</span></div><div data-questions-questionid="2" class="paddedDiv">Which of the following browsers do you use while developing websites?<br><input type="checkbox" data-choices-questionid="2" data-choices-choiceid="4"><span>IE9</span><br><input type="checkbox" data-choices-questionid="2" data-choices-choiceid="5"><span>Firefox</span><br><input type="checkbox" data-choices-questionid="2" data-choices-choiceid="6"><span>Chrome</span></div><div data-questions-questionid="3" class="paddedDiv">Which of the following tools do you use?<br><input type="checkbox" data-choices-questionid="3" data-choices-choiceid="7"><span>Visual Studio</span><br><input type="checkbox" data-choices-questionid="3" data-choices-choiceid="8"><span>Web Matrix</span><br><input type="checkbox" data-choices-questionid="3" data-choices-choiceid="9"><span>Expression Web</span></div>"} 

をエラー:

{"After parsing a value an unexpected character was encountered: 1. Path 'container', line 1, position 105."} 

問題はどこですか?

JavascriptをSubmitData

function SubmitData(event) { 
     var data = ''; 
     for (var i = 0; i < storage.length; i++) { 
     var key = storage.key(i); 
     var value = storage[key]; 
     var pair = '"' + key + '":"' + value + '"'; 
     data = data + pair + ","; 
     } 
     if (data.charAt(data.length - 1) == ',') { 
     data = data.substring(0, data.length - 1) 
     } 
     data = '{' + data + '}'; 
     $.ajax({ 
     type: "POST", 
     url: "/Home/SaveResults", 
     contentType: "application/json; charset=utf-8", 
     data: data, 
     dataType: "json", 
     success: function(results){ 
      alert('Results saved!'); 
      window.localStorage.clear(); 
     }, 
     error: function (err) { 
      alert(err.status + " - " + err.statusText); 
     } 
     }) 
    } 

答えて

0

があなたのJSONデータを見てください:

"container":"<div data-questions-questionid="1 

プロパティの値が 'コンテナ' は右の '1' 前に終わるそうです...それはエンコーディングですエラーも他の問題があるかどうかはわかりません。