保存从Windows Phone背后的代码创建的Json文件
我使用名为Newtonsoft.Json的dll在代码背后编写Json 这是我的密码:保存从Windows Phone背后的代码创建的Json文件,json,windows-phone-7,isolatedstorage,json.net,Json,Windows Phone 7,Isolatedstorage,Json.net,我使用名为Newtonsoft.Json的dll在代码背后编写Json 这是我的密码: StringBuilder stringBuilder = new StringBuilder(); StringWriter stringWriter = new StringWriter(stringBuilder); JsonWriter jsonWriter = new JsonTextWriter(stringWriter); jsonWriter.
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保存在沙盒中创建的文件?我已经编写了一个简短的代码以供您的帮助。我希望这有帮助
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对象尝试将jsonWriter的对象存储在独立的存储中。请参见此处的类似解决方案: