为了您的帮助,我写了一个简短的代码。我希望这有帮助
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 对象