2012-01-31 19 views
7

Próbowałem różnych sposobów analizowania json w Windows 8 i otrzymuję ten błąd dużo.Parsowanie Json Windows8

„WinRT informacje: WEB_E_INVALID_JSON_STRING”

Robi się do punktu, że mogę używać tego samego json ciąg, i to działa (bardziej lub mniej) jeśli czytam z nim z internetu, ale to nie będzie działać jeśli czytam go z lokalnego pliku.

herezje kod do zapoznania go z sieci:

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(); 

     } 

    } 

Tak to działa, ale jeśli mogę użyć tej samej metody analizowania i użyć tego kodu, aby uzyskać plik z lokalnego pliku w moim app jeśli failswith błędu "Informacje 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); 
     } 
    } 
} 

Ktoś może mi pomóc, jeśli to błąd z wersji zapoznawczej lub coś, czego mi brakuje ?!

Z góry dziękuję

+0

Prosty test jednostkowy nie działa? – leppie

+0

Czy jesteś absolutnie pewien, że to ten sam ciąg? Jakie kodowanie wykorzystuje twój plik? –

+0

tak jest to samo ciąg .. Porównałem visualy i w kodzie jak ciąg xFromserver == ciąg yFromlocal jest true ... Zastanawiam się, czy może to być coś z typem filee, gdy czytam plik z lokalnego źródła .. wydaje się, że nie jest to jedyny problem: http://phil-it.org/chris/?p=769 –

Odpowiedz

1

Wymyśliłem to. Kiedy odczytuję plik JSON z pliku, metoda, której używam do odczytu tego pliku, kopiuje znak UTF8 ByteOrderMark do strumienia wynikowego. W rezultacie, kiedy wywołuję stringFromFile.equals (hardcodedString), zwracana jest wartość false. Użyłem następującego kodu, aby odczytać tekst z pliku i usunąć zestawienie komponentów, a teraz parsować JSON przy użyciu Windows.Data.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; 

     }