2016-10-01 84 views
0

我在文件中添加了Json添加到我的項目嵌入式資源。 我想讀取包含某個對象列表的這個文件並反序列化它。將json對象展開爲對象數組

我有以下JSON文件:

[ 
    { 
     "Status": 21, 
     "CustomerId": "e3633ccb-bbea-465d-9ce6-6c37e9c40e2e" 
    }, 
    { 
     "Status": 20, 
     "CustomerId": "d02e2970-7c28-41b0-89f3-5276a97e12c9" 
    } 
] 

及以下型號:

public class CustomerStatus 
{ 
    public int Status { get; set; } 
    public string CustomerId { get; set; } 
} 

,因爲我讀從資源JSON文件,它會自動在字節數組的形式和我轉換它串起來,它有\ r \ n和\ t在裏面。
在我的代碼我有以下行做實現這一目標,但它失敗:

var string customerdata = System.Text.Encoding.UTF8.GetString(myResources.CustomerStatus); 
var data = JsonConvert.DeserializeObject<List<CustomerStatus>>(customerdata); 

我收到此錯誤: 扔類型的異常「Newtonsoft.Json.JsonReaderException」消息「,而解析值意外的性格遇到: 。路徑'',第0行,位置0。

UPDATE:

以下行也導致了同樣的問題:

var string customerdata = System.Text.Encoding.UTF8.GetString(myResources.CustomerStatus); 
    .Replace("\r\n", " ") 
    .Replace("\t", " "); 
+2

我敢打賭你會在文件的開頭有一個字節順序標記,它會在字符串中結束。檢查'customerdata'的第一個字符的字符代碼是否爲0xFEFF。 –

回答

1

這似乎是一個文件的編碼問題。您可以使用Notepad ++來轉換文件,或者您可以使用記事本打開JSON文件,並使用另存爲選擇ANSI作爲編碼。然後取出並在資源再次添加文件並切換 System.Text.Encoding.UTF8.GetString(myResources.CustomerStatus)

System.Text.Encoding.ASCII.GetString(myResources.CustomerStatus)

如果你是熱衷於使用UTF8,然後用記事本++轉換文件來代替。

+0

超級!它解決了!謝啦! –

1

嘗試使用網絡庫的方法

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Web; 
using System.Web.Script.Serialization; 

namespace ConsoleApplication16 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string customerdata = 
       "[" + 
        "{" + 
         "\"Status\": 21," + 
         "\"CustomerId\": \"e3633ccb-bbea-465d-9ce6-6c37e9c40e2e\"" + 
        "}," + 
        "{" + 
         "\"Status\": 20," + 
         "\"CustomerId\": \"d02e2970-7c28-41b0-89f3-5276a97e12c9\"" + 
        "}" + 
       "]"; 

      JavaScriptSerializer ser = new JavaScriptSerializer(); 
      var r = ser.Deserialize<List<CustomerStatus>>(customerdata); 

     } 

    } 
    public class CustomerStatus 
    { 
     public int Status { get; set; } 
     public string CustomerId { get; set; } 
    } 
} 
+0

我認爲把文件放在資源中會更整潔,更具可讀性。你知道它爲什麼會失敗嗎? –

0

嘗試以下方法爲嵌入式資源:

using Newtonsoft.Json; 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Reflection; 

namespace StackOverflowTests { 
    class Program { 
    static void Main(string[] args) { 
     var assembly = Assembly.GetExecutingAssembly(); 
     var resource = "StackOverflowTests.StatusCustomer.json"; 

     var deserializedResource = GetEmbeddedResource<List<CustomerStatus>>(assembly, resource); 

     Console.WriteLine(JsonConvert.SerializeObject(deserializedResource)); 
     Console.ReadKey(); 
    } 

    private static T GetEmbeddedResource<T>(Assembly assembly, string resource) { 
     using (Stream manifestStream = assembly.GetManifestResourceStream(resource)) 
     using (StreamReader reader = new StreamReader(manifestStream)) { 
     string result = reader.ReadToEnd(); 
     return JsonConvert.DeserializeObject<T>(result); 
     } 
    } 

    public class CustomerStatus { 
     public int Status { get; set; } 
     public string CustomerId { get; set; } 
    } 
    } 
} 

如果你不能使用說明後

System.Text.Encoding.UTF8.GetString(myResources.CustomerStatus) 

呼叫方法,崗位序列化的字符串,所以我可以看看什麼是錯的。