我为Android编写应用程序,需要将json写入.txt文件。 .txt文件位于项目根目录
现在我像这样解析json
string url2 = "http://new.murakami.ua/?mkapi=getProducts";
JsonValue json = await FetchAsync(url2);
private async Task<JsonValue> FetchAsync(string url)
{
// Create an HTTP web request using the URL:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
request.ContentType = "application/json";
request.Method = "GET";
// Send the request to the server and wait for the response:
using (WebResponse response = await request.GetResponseAsync())
{
// Get a stream representation of the HTTP web response:
using (Stream stream = response.GetResponseStream())
{
// Use this stream to build a JSON document object:
JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));
//dynamic data = JObject.Parse(jsonDoc[15].ToString);
Console.Out.WriteLine("Response: {0}", jsonDoc.ToString());
// Return the JSON document:
return jsonDoc;
}
我试着用这个:
StreamWriter sw = new StreamWriter(@"\myfile.txt");
sw.Write(json.ToString());
sw.Close();
但它说System.UnauthorizedAccessException: Access to the path "/\myfile.txt" is denied.
如何解析json到文件?
答案 0 :(得分:0)
尝试更改应用程序私人目录的路径
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string filename = System.IO.Path.Combine(path, "myfile.txt");
using (var streamWriter = new StreamWriter(filename, true))
{
//Write your data here
}
希望这有帮助