2010-05-14 18 views
6

Bir I18N projesine katkıda bulunuyorum ve * .resx dosyalarını JSON nesneleri olarak (herhangi bir nedenle) serileştirmek için bir çağrı var.ASP.NET * .resx serileştirme

Ne merak ediyorum geçerli:

  • biz belirteçleri kapmak için HttpContext.GetGlobalResourceObject kullanabilir böylece belirli bir * .Resx dosya için geçerli anahtarların bir listesini almak için bir yolu var mı ?
  • Eğer bu işe yaramazsa, kimse akıllı bir çözüm bulmuş mu?
+0

JSON verisine ve AJAX çağrılarına dayanan bir uygulama oluşturuyor olmanızın bir nedeni, C# View dosyalarınızın yardımı olmadan Javascript'te yerelleştirilmiş html snippet'leri oluşturmanız gerekebilir. Bu durumda, reseks verisine sahip JSON nesnesi geçersizdir. – sonjz

cevap

8
Sub ReadRessourceFile() 
     ''#Requires Assembly System.Windows.Forms 
     Dim rsxr As System.Resources.ResXResourceReader = New System.Resources.ResXResourceReader("items.resx") 

     ''# Iterate through the resources and display the contents to the console.  
     Dim d As System.Collections.DictionaryEntry 
     For Each d In rsxr 
      Console.WriteLine(d.Key.ToString() + ":" + ControlChars.Tab + d.Value.ToString()) 
     Next d 

     ''#Close the reader. 
     rsxr.Close() 
    End Sub 

O zaman sonra System.Web.Extensions.dll

kullanarak JSON serialize bir Serializable Sözlük, bu eklemeniz gerekir
Public Class JSONHelper 

Public Shared Function Serialize(Of T)(ByVal obj As T) As String 
    Dim JSONserializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer() 
    Return JSONserializer.Serialize(obj) 
End Function 

Public Shared Function Deserialize(Of T)(ByVal json As String) As T 
    Dim obj As T = Activator.CreateInstance(Of T)() 
    Dim JSONserializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer() 
    obj = JSONserializer.Deserialize(Of T)(json) 
    Return obj 
End Function 

End Class 

Düzenleme: C#:

public void ReadRessourceFile() 
{ 
    //Requires Assembly System.Windows.Forms ' 
    System.Resources.ResXResourceReader rsxr = new System.Resources.ResXResourceReader("items.resx"); 

    // Iterate through the resources and display the contents to the console. '  
    System.Collections.DictionaryEntry d = default(System.Collections.DictionaryEntry); 
    foreach (DictionaryEntry d_loopVariable in rsxr) { 
     d = d_loopVariable; 
     Console.WriteLine(d.Key.ToString() + ":" + ControlChars.Tab + d.Value.ToString()); 
    } 

    //Close the reader. ' 
    rsxr.Close(); 
} 

Ve JSON yardımcısı:

public class JSONHelper 
{ 

    public static string Serialize<T>(T obj) 
    { 
     System.Web.Script.Serialization.JavaScriptSerializer JSONserializer = new System.Web.Script.Serialization.JavaScriptSerializer(); 
     return JSONserializer.Serialize(obj); 
    } 

    public static T Deserialize<T>(string json) 
    { 
     T obj = Activator.CreateInstance<T>(); 
     System.Web.Script.Serialization.JavaScriptSerializer JSONserializer = new System.Web.Script.Serialization.JavaScriptSerializer(); 
     obj = JSONserializer.Deserialize<T>(json); 
     return obj; 
    } 

} 
+1

JFYI, SO kullanıcılarının çoğu C# yi daha iyi anlıyor – abatishchev

+0

Sonra, çoğu SO kullanıcısı bir şeyler öğreniyor. –

+1

Yep, "gizli" ResXResourceReader sınıfında yerleşik kullanıyor. – Greg