本文整理汇总了C#中JSONObject.stringForKey方法的典型用法代码示例。如果您正苦于以下问题:C# JSONObject.stringForKey方法的具体用法?C# JSONObject.stringForKey怎么用?C# JSONObject.stringForKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JSONObject
的用法示例。
在下文中一共展示了JSONObject.stringForKey方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: simpleTest
public void simpleTest()
{
//Create a simple JSON object
JSONObject json1 = new JSONObject("{id:1, \"name\": \"Chris Richards\"}");
Assert.AreEqual(1, json1["id"], "Getting int with indexer");
Assert.AreEqual("Chris Richards", json1["name"], "Getting string with indexer");
Assert.IsNull(json1["rank"], "Keys that don't exist should return null");
Assert.AreEqual("1", json1.stringForKey("id"), "stringForKey should convert type if possible");
Assert.AreEqual("Chris Richards", json1.stringForKey("name"), "Getting string with stringForKey");
Assert.AreEqual(string.Empty, json1.stringForKey("rank"), "stringForKey should return empty if the key doesn't exist");
Assert.AreEqual(1, json1.intForKey("id"), "intForKey should return an int");
Assert.Throws<FormatException>(delegate() { json1.intForKey("name"); }, "Exception if convert fails.");
Assert.AreEqual(0, json1.intForKey("rank"), "Zero if key not found");
}
示例2: typeTest
public void typeTest()
{
//Create a JSON object with all the types
JSONObject json1 = new JSONObject("{id:1, \"name\": \"Chris Richards\", isTrue:true, isFalse:false, list:[true, false, null], object:{gum:\"Trident\", type:\"Spearmint\"}}");
Assert.AreEqual(1, json1.intForKey("id"));
Assert.AreEqual("Chris Richards", json1.stringForKey("name"));
Assert.IsTrue(json1.boolForKey("isTrue"));
Assert.IsFalse(json1.boolForKey("isFalse"));
Assert.IsInstanceOf(typeof(System.Collections.Generic.List<object>), json1.listForKey("list"));
Assert.IsInstanceOf<JSONObject>(json1.objectForKey("object"));
System.Collections.Generic.List<object> list = json1.listForKey("list");
Assert.AreEqual(3, list.Count);
Assert.AreEqual(true, list[0]);
Assert.AreEqual(false, list[1]);
Assert.AreEqual(null, list[2]);
JSONObject obj = json1.objectForKey("object");
Assert.AreEqual("Trident", obj.stringForKey("gum"));
Assert.AreEqual("Spearmint", obj.stringForKey("type"));
}