2012-11-27 41 views
1

我寫一個JSON在後面的代碼使用DLL稱爲Newtonsoft.Json保存文件的JSON代碼的Windows Phone背後創建

這裏是我的代碼:

StringBuilder stringBuilder = new StringBuilder(); 
     StringWriter stringWriter = new StringWriter(stringBuilder); 
     JsonWriter jsonWriter = new JsonTextWriter(stringWriter); 

     jsonWriter.Formatting = Formatting.Indented; 
     jsonWriter.WriteStartObject(); 
     jsonWriter.WritePropertyName("name"); 
     jsonWriter.WriteRawValue(textboxNomeCompleto.Text); 
     jsonWriter.WritePropertyName("user"); 
     jsonWriter.WriteValue(textboxEmail.Text); 
     jsonWriter.WritePropertyName("password"); 
     jsonWriter.WriteValue(textboxSenha.Text); 
     jsonWriter.WriteEndObject(); 

如何節省使用沙箱中創建的文件IsolatedStoreFile ???

+0

嘗試存儲jsonWriter的對象在隔離存儲 – Harshit

+0

在這裏看到一個類似的解決方案: http://stackoverflow.com/questions/5980403/wiring-json-using-newtonsoft-json-jsontextwriter – Senthil

回答

0

我寫了一個簡短的代碼來幫助你。我希望這有助於

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using Microsoft.Phone.Controls; 
using Newtonsoft.Json; 
using System.Text; 
using System.IO; 
using System.IO.IsolatedStorage; 

using System.Threading; 
using System.Xml; 
using System.Xml.Serialization; 
namespace jsontest 
{ 
    public partial class MainPage : PhoneApplicationPage 
    { 
     // Constructor 
     public MainPage() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      StringBuilder stringBuilder = new StringBuilder(); 
      StringWriter stringWriter = new StringWriter(stringBuilder); 
      JsonWriter jsonWriter = new JsonTextWriter(stringWriter); 

      jsonWriter.Formatting = Formatting.Indented; 
      jsonWriter.WriteStartObject(); 
      jsonWriter.WritePropertyName("name"); 
      jsonWriter.WriteRawValue("Test"); 
      jsonWriter.WritePropertyName("user"); 
      jsonWriter.WriteValue("T123"); 
      jsonWriter.WritePropertyName("password"); 
      jsonWriter.WriteValue("StackOverFlow123"); 
      jsonWriter.WriteEndObject(); 
      stringWriter.Close(); 

      saveOnFile(stringWriter.ToString()); 
      readFromFile(); 
     } 

     public void saveOnFile(string data) 
     { 
      IsolatedStorageFile myFile = IsolatedStorageFile.GetUserStoreForApplication(); 
      StreamWriter sw = new StreamWriter(new IsolatedStorageFileStream("Test.txt", FileMode.Create, myFile)); 
     sw.WriteLine(data); //Wrting to the file 
     sw.Close(); 

     } 
     public void readFromFile() 
     { 
      IsolatedStorageFile myFile = IsolatedStorageFile.GetUserStoreForApplication(); 
      try 
      { 

       StreamReader reader = new StreamReader(new IsolatedStorageFileStream("Test.txt", FileMode.Open, myFile)); 
       string rawData = reader.ReadToEnd(); 
       reader.Close(); 
       textBlock1.Text = rawData;//use raw data as string of JSON data 
      } 
      catch { } 
     } 
    } 
} 

,這是一個工作代碼,RAWDATA必須解析,然後再使用JSON對象