1

我使用名为 Newtonsoft.Json 的 dll 在后面的代码中编写了一个 Json

这是我的代码:

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 保存在SandBox 中创建的文件???

4

1 回答 1

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 对象

于 2012-11-28T13:55:01.557 回答