18 NİSAN 2010, Pazar
Nasıl C#.NET biçimlendirilmiş json yapabilirim
Kullanıyorum .Ve benim config seri hale getirmek istiyorum yani okunabilir yerine NET json parser dosya
{"blah":"v", "blah2":"v2"}
bir şey daha hoş gibi
{
"blah":"v",
"blah2":"v2"
}
benim kod gibi bir şey
using System.Web.Script.Serialization;
var ser = new JavaScriptSerializer();
configSz = ser.Serialize(config);
using (var f = (TextWriter)File.CreateText(configFn))
{
f.WriteLine(configSz);
f.Close();
}
CEVAP
18 NİSAN 2010, Pazar
Zor zamanlar Javascriptserializeryapıcı ile bunu başarmak zorundasın.
JSON.Net deneyin.
Json.net örnekte küçük değişiklikler ile
using System;
using Newtonsoft.Json;
namespace JsonPrettyPrint
{
internal class Program
{
private static void Main(string[] args)
{
Product product = new Product
{
Name = "Apple",
Expiry = new DateTime(2008, 12, 28),
Price = 3.99M,
Sizes = new[] { "Small", "Medium", "Large" }
};
string json = JsonConvert.SerializeObject(product, Formatting.Indented);
Console.WriteLine(json);
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);
}
}
internal class Product
{
public String[] Sizes { get; set; }
public decimal Price { get; set; }
public DateTime Expiry { get; set; }
public string Name { get; set; }
}
}
Sonuçları
{
"Sizes": [
"Small",
"Medium",
"Large"
],
"Price": 3.99,
"Expiry": "\/Date(1230447600000-0700)\/",
"Name": "Apple"
}
Belgeleri:Serialize an Object
Bunu Paylaş:
Nasıl Gıt (chmod) dosya mod değişiklik...
Nasıl Krom XML yerine JSON dönmek için...
Nasıl terminalden Curl ile JSON veri g...
Nasıl yeniden yükleme yapabilirim .çık...
Nasıl ayrıştırma kullanarak Node.js JS...