JSON konusunu biraz daha irdelerken bulduklarım:
toJSONString() Metodu ile JSON objesinin string olarak görüntülenmesi
// josn.js must be included prior to this point var continents = new Array(); continents.push("Europe"); continents.push("Asia"); continents.push("Australia"); continents.push("Antarctica"); continents.push("North America"); continents.push("South America"); continents.push("Africa"); alert("The JSON representation of the continents array is: " + continents.toJSONString());
var mailingAddress = { "Address" : "123 Anywhere St.", "City" : "Springfield", "PostalCode" : 99999 }; alert("The package will be shipped to postal code " + mailingAddress.PostalCode);
eval() ile JSON stringini parse etmek
var value = eval( "(" + jsonText + ")" );
var arrayAsJSONText = '["Europe", "Asia", "Australia", "Antarctica", "North America", "South America", "Africa"]'; var continents = eval( arrayAsJSONText ); alert(continents[0] + " is one of the " + continents.length + " continents.");
Yinede tavsiye edilen PARSE metodu(json.js dosyasını dahil ederek):
var continents = arrayAsJSONText.parseJSON();
C# ile JSON oluşturmak
JsonWriter : WriteStartArray, WriteString, WriteEndArray,WriteNumber, WriteBoolean, WriteNull, WriteStartObject, WriteEndObject, WriteMember
using (JsonWriter w = new JsonTextWriter(Console.Out)) { w.WriteStartObject(); // { w.WriteMember("Name"); // "Name" : w.WriteString("John Doe"); // "John Doe", w.WriteMember("PermissionToCall"); // "PermissionToCall" : w.WriteBoolean(true); // true, w.WriteMember("PhoneNumbers"); // "PhoneNumbers" : w.WriteStartArray(); // [ w.WriteStartObject(); // { w.WriteMember("Location"); // "Location" : w.WriteString("Home"); // "Home", w.WriteMember("Number"); // "Number" : w.WriteString("555-555-1234"); // "555-555-1234" w.WriteEndObject(); // }, w.WriteStartObject(); // { w.WriteMember("Location"); // "Location" : w.WriteString("Work"); // "Work", w.WriteMember("Number"); // "Number" : w.WriteString("555-555-9999"); // "555-555-9999" w.WriteEndObject(); // } w.WriteEndArray(); // ] w.WriteEndObject(); // } }
JsonConvert.ExportToString
string[] continents = { "Europe", "Asia", "Australia", "Antarctica", "North America", "South America", "Africa" }; string jsonText = JsonConvert.ExportToString(continents);
Parsing JSON Text with JsonTextReader
string jsonText = @"[""Europe"", ""Asia"", ""Australia"", ""Antarctica"", ""North America"", ""South America"", ""Africa""]"; using (JsonTextReader reader = new JsonTextReader(new StringReader(jsonText))) { while (reader.Read()) { if (reader.TokenClass == JsonTokenClass.String && reader.Text.StartsWith("A")) { Console.WriteLine(reader.Text); } } }
Hiç yorum yok:
Yorum Gönder