2012-01-31 101 views
7

我已經嘗試了多種方法來解析JSON在Windows 8中,我得到這個錯誤很多。解析JSON Windows8

「WinRT的信息:WEB_E_INVALID_JSON_STRING」

它得到的是我用同樣的JSON字符串的一個點,它的作品(或多或少),如果我從它從網上閱讀,但它不會工作如果我從本地文件讀取它。

繼承人的代碼從網上閱讀:

public async void ExamineJson() 
    { 
     string responseText = await GetjsonStream(); 
     ParseJson(responseText); 
    } 


public async Task<string> GetjsonStream() 
    { 
     HttpClient client = new HttpClient(); 
     string url = "http://rmarinho.facilit.us/app/d/rtp/config.json"; 
     HttpResponseMessage response = await client.GetAsync(url); 
     return response.Content.ReadAsString(); 
    } 



    private static void ParseJson(string responseText) 
    { 
     JsonObject root = new JsonObject(responseText); 
     string epg = root.GetNamedString("epgurl"); 

     JsonArray themes = root["themes"].GetArray(); 

     for (int i = 0; i < themes.Count; i++) 
     { 

      JsonObject section = themes[i].GetObject(); 

     } 

    } 

所以此工程,但如果failswith的錯誤,如果我用同樣的解析方法,並使用此代碼從本地文件的文件在我的應用「WinRT信息:WEB_E_INVALID_JSON_STRING」。

FileSync.Read<string>(installedLocation, "config.json", 
      (fileSize, reader) => 
       reader.ReadString(fileSize), 
       responseText => 
       { 
        ParseJson(responseText); 

       }) 

    public static class FileSync 
{ 
    public static async void Read<TDocument>(StorageFolder folder, string fileName, 
     Func<uint, DataReader, TDocument> reader, Action<TDocument> completion = null) 
    { 


     StorageFile file; 
     IRandomAccessStream stream; 
     IInputStream inputStream; 
     DataReader dr; 

     file = await folder.GetFileAsync(fileName); 

     stream = await file.OpenAsync(FileAccessMode.Read); 
     inputStream = stream.GetInputStreamAt(0); 

     uint fileSize = (uint)stream.Size; 

     dr = new DataReader(inputStream); 
     await dr.LoadAsync(fileSize); 

     Task<TDocument> task = new Task<TDocument>(() => reader(fileSize, dr)); 
     task.Start(); 
     TDocument doc = await task; 

     if (completion != null) 
     { 
      completion(doc); 
     } 
    } 

    public static async void Write<TDocument>(StorageFolder folder, string fileName, 
CreationCollisionOption collisionOption, TDocument doc, 
Action<DataWriter, TDocument> writer, Action<bool> complete = null) 
    { 
     StorageFile creator; 
     IRandomAccessStream stream; 
     IOutputStream outputStream; 
     DataWriter dw; 

     creator = await folder.CreateFileAsync(fileName, collisionOption); 

     stream = await creator.OpenAsync(FileAccessMode.ReadWrite); 
     outputStream = stream.GetOutputStreamAt(0); 

     dw = new DataWriter(outputStream); 

     Task task = new Task(() => writer(dw, doc)); 
     task.Start(); 
     await task; 

     await dw.StoreAsync(); 
     bool success = await outputStream.FlushAsync(); 
     if (complete != null) 
     { 
      complete(success); 
     } 
    } 
} 

任何人都可以幫助我出圖,如果這是從預覽版本的錯誤或是我失蹤的東西?!

在此先感謝

+0

一個簡單的單元測試不起作用? – leppie 2012-02-01 19:29:57

+0

你確定它是相同的字符串嗎?你的文件使用什麼編碼? – 2012-02-01 21:29:47

+0

肯定是相同的字符串..我有visualy比較,像串碼xFromserver ==串yFromlocal是真的...... 我想知道如果itmaybe一些與文件contente類型,當我讀到從本地源文件.. 看來我不是唯一一個問題:http://phil-it.org/chris/?p=769 – 2012-02-02 15:38:45

回答

1

想通了。當我從文件中讀取JSON時,我用來讀取文件的方法是將UTF8 ByteOrderMark字符複製到結果流中。因此,當我調用stringFromFile.equals(hardcodedString)時返回false。我使用以下代碼從文件中讀取文本並剝離BOM,然後使用Windows.Data.Json工作解析JSON。

private readonly static string UTF8_BYTE_ORDER_MARK = 
    Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble(), 0, Encoding.UTF8.GetPreamble().Length); 


     private string GetStringContentsOfFile(string path) 
     { 
      Uri filePath = new Uri(path); 
      var jsonFileTask = StorageFile.GetFileFromApplicationUriAsync(filePath).AsTask(); 
      jsonFileTask.Wait(); 
      var jsonFile = jsonFileTask.Result; 

      var getStringContentsTask = FileIO.ReadTextAsync(jsonFile, Windows.Storage.Streams.UnicodeEncoding.Utf8).AsTask(); 
      getStringContentsTask.Wait(); 
      var text = getStringContentsTask.Result; 

      // FileIO.ReadTextAsync copies the UTF8 byte order mark into the result string. Strip the byte order mark 
      text = text.Trim(UTF8_BYTE_ORDER_MARK.ToCharArray()); 

      return text; 

     }