本文整理汇总了C#中JObject.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# JObject.ToString方法的具体用法?C# JObject.ToString怎么用?C# JObject.ToString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JObject
的用法示例。
在下文中一共展示了JObject.ToString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Example
public void Example()
{
#region Usage
JObject o = new JObject
{
{ "name1", "value1" },
{ "name2", "value2" }
};
JsonWriter writer = o.CreateWriter();
writer.WritePropertyName("name3");
writer.WriteStartArray();
writer.WriteValue(1);
writer.WriteValue(2);
writer.WriteEndArray();
Console.WriteLine(o.ToString());
// {
// "name1": "value1",
// "name2": "value2",
// "name3": [
// 1,
// 2
// ]
// }
#endregion
}
示例2: Example
public void Example()
{
#region Usage
JObject o = new JObject
{
{ "Cpu", "Intel" },
{ "Memory", 32 },
{
"Drives", new JArray
{
"DVD",
"SSD"
}
}
};
Console.WriteLine(o.ToString());
// {
// "Cpu": "Intel",
// "Memory": 32,
// "Drives": [
// "DVD",
// "SSD"
// ]
// }
#endregion
}
示例3: WritePropertyWithNoValue
public void WritePropertyWithNoValue()
{
var o = new JObject();
o.Add(new JProperty("novalue"));
StringAssert.Equal(@"{
""novalue"": null
}", o.ToString());
}
示例4: Example
public void Example()
{
#region Usage
List<Post> posts = GetPosts();
JObject rss =
new JObject(
new JProperty("channel",
new JObject(
new JProperty("title", "James Newton-King"),
new JProperty("link", "http://james.newtonking.com"),
new JProperty("description", "James Newton-King's blog."),
new JProperty("item",
new JArray(
from p in posts
orderby p.Title
select new JObject(
new JProperty("title", p.Title),
new JProperty("description", p.Description),
new JProperty("link", p.Link),
new JProperty("category",
new JArray(
from c in p.Categories
select new JValue(c)))))))));
Console.WriteLine(rss.ToString());
// {
// "channel": {
// "title": "James Newton-King",
// "link": "http://james.newtonking.com",
// "description": "James Newton-King's blog.",
// "item": [
// {
// "title": "Json.NET 1.3 + New license + Now on CodePlex",
// "description": "Annoucing the release of Json.NET 1.3, the MIT license and being available on CodePlex",
// "link": "http://james.newtonking.com/projects/json-net.aspx",
// "category": [
// "Json.NET",
// "CodePlex"
// ]
// },
// {
// "title": "LINQ to JSON beta",
// "description": "Annoucing LINQ to JSON",
// "link": "http://james.newtonking.com/projects/json-net.aspx",
// "category": [
// "Json.NET",
// "LINQ"
// ]
// }
// ]
// }
// }
#endregion
}
示例5: Example
public void Example()
{
#region Usage
JObject videogameRatings = new JObject(
new JProperty("Halo", 9),
new JProperty("Starcraft", 9),
new JProperty("Call of Duty", 7.5));
File.WriteAllText(@"c:\videogames.json", videogameRatings.ToString());
// write JSON directly to a file
using (StreamWriter file = File.CreateText(@"c:\videogames.json"))
using (JsonTextWriter writer = new JsonTextWriter(file))
{
videogameRatings.WriteTo(writer);
}
#endregion
}
示例6: Example
public void Example()
{
#region Usage
JArray array = new JArray();
array.Add("Manual text");
array.Add(new DateTime(2000, 5, 23));
JObject o = new JObject();
o["MyArray"] = array;
string json = o.ToString();
// {
// "MyArray": [
// "Manual text",
// "2000-05-23T00:00:00"
// ]
// }
#endregion
}
示例7: CreatingJson
public void CreatingJson()
{
#if NETFX_CORE
#region CreatingJSON
// Windows.Data.Json
// -----------------
JsonObject jsonObject = new JsonObject
{
{"CPU", JsonValue.CreateStringValue("Intel")},
{
"Drives", new JsonArray
{
JsonValue.CreateStringValue("DVD read/writer"),
JsonValue.CreateStringValue("500 gigabyte hard drive")
}
}
};
string json1 = jsonObject.Stringify();
// LINQ to JSON
// ------------
JObject jObject = new JObject
{
{"CPU", "Intel"},
{
"Drives", new JArray
{
"DVD read/writer",
"500 gigabyte hard drive"
}
}
};
string json2 = jObject.ToString();
#endregion
#endif
}
示例8: Example
public void Example()
{
#region Usage
JObject o1 = new JObject
{
{ "String", "A string!" },
{ "Items", new JArray(1, 2) }
};
Console.WriteLine(o1.ToString());
// {
// "String": "A string!",
// "Items": [
// 1,
// 2
// ]
// }
JObject o2 = (JObject)o1.DeepClone();
Console.WriteLine(o2.ToString());
// {
// "String": "A string!",
// "Items": [
// 1,
// 2
// ]
// }
Console.WriteLine(JToken.DeepEquals(o1, o2));
// true
Console.WriteLine(Object.ReferenceEquals(o1, o2));
// false
#endregion
}
示例9: CreateJTokenTreeNested
public void CreateJTokenTreeNested()
{
List<Post> posts = GetPosts();
JObject rss =
new JObject(
new JProperty("channel",
new JObject(
new JProperty("title", "James Newton-King"),
new JProperty("link", "http://james.newtonking.com"),
new JProperty("description", "James Newton-King's blog."),
new JProperty("item",
new JArray(
from p in posts
orderby p.Title
select new JObject(
new JProperty("title", p.Title),
new JProperty("description", p.Description),
new JProperty("link", p.Link),
new JProperty("category",
new JArray(
from c in p.Categories
select new JValue(c)))))))));
StringAssert.AreEqual(@"{
""channel"": {
""title"": ""James Newton-King"",
""link"": ""http://james.newtonking.com"",
""description"": ""James Newton-King's blog."",
""item"": [
{
""title"": ""Json.NET 1.3 + New license + Now on CodePlex"",
""description"": ""Annoucing the release of Json.NET 1.3, the MIT license and being available on CodePlex"",
""link"": ""http://james.newtonking.com/projects/json-net.aspx"",
""category"": [
""Json.NET"",
""CodePlex""
]
},
{
""title"": ""LINQ to JSON beta"",
""description"": ""Annoucing LINQ to JSON"",
""link"": ""http://james.newtonking.com/projects/json-net.aspx"",
""category"": [
""Json.NET"",
""LINQ""
]
}
]
}
}", rss.ToString());
var postTitles =
from p in rss["channel"]["item"]
select p.Value<string>("title");
Assert.AreEqual("Json.NET 1.3 + New license + Now on CodePlex", postTitles.ElementAt(0));
Assert.AreEqual("LINQ to JSON beta", postTitles.ElementAt(1));
var categories =
from c in rss["channel"]["item"].Children()["category"].Values<string>()
group c by c
into g
orderby g.Count() descending
select new { Category = g.Key, Count = g.Count() };
Assert.AreEqual("Json.NET", categories.ElementAt(0).Category);
Assert.AreEqual(2, categories.ElementAt(0).Count);
Assert.AreEqual("CodePlex", categories.ElementAt(1).Category);
Assert.AreEqual(1, categories.ElementAt(1).Count);
Assert.AreEqual("LINQ", categories.ElementAt(2).Category);
Assert.AreEqual(1, categories.ElementAt(2).Count);
}
示例10: RawChildValues
public void RawChildValues()
{
var o = new JObject();
o["val1"] = new JRaw("1");
o["val2"] = new JRaw("1");
string json = o.ToString();
StringAssert.Equal(@"{
""val1"": 1,
""val2"": 1
}", json);
}
示例11: ArrayOrder
public void ArrayOrder()
{
string itemZeroText = "Zero text";
IEnumerable<ListItemFields> t = new List<ListItemFields>
{
new ListItemFields {ListItemText = "First", ListItemValue = 1},
new ListItemFields {ListItemText = "Second", ListItemValue = 2},
new ListItemFields {ListItemText = "Third", ListItemValue = 3}
};
JObject optionValues =
new JObject(
new JProperty("options",
new JArray(
new JObject(
new JProperty("text", itemZeroText),
new JProperty("value", "0")),
from r in t
orderby r.ListItemValue
select new JObject(
new JProperty("text", r.ListItemText),
new JProperty("value", r.ListItemValue.ToString())))));
string result = "myOptions = " + optionValues.ToString();
Assert.AreEqual(@"myOptions = {
""options"": [
{
""text"": ""Zero text"",
""value"": ""0""
},
{
""text"": ""First"",
""value"": ""1""
},
{
""text"": ""Second"",
""value"": ""2""
},
{
""text"": ""Third"",
""value"": ""3""
}
]
}", result);
}
示例12: Invoke
public static object Invoke(MethodInfo methodInfo, JObject json)
{
ParameterInfo[] paramterInfos = methodInfo.GetParameters();
var type = methodInfo.DeclaringType;
object[] paramters = new object[paramterInfos.Length];
try
{
for (int i = 0; i < paramterInfos.Length; i++)
{
Type parameterType = paramterInfos[i].ParameterType;
string parameterName = paramterInfos[i].Name;
object value = null;
JToken jvalue = null;
if (json.TryGetValue(parameterName, StringComparison.OrdinalIgnoreCase, out jvalue))
{
if (parameterType == typeof(string))
value = jvalue.ToString();
else
value = jvalue.ToObject(parameterType);
}
else
{
if (parameterType == typeof(string))
value = json.ToString();
else
value = json.ToObject(parameterType);
}
paramters[i] = value;
}
}
catch (Exception ex)
{
throw new Exception("解析方法'" + type.FullName + "." + methodInfo.Name + "'参数出错,请检查传入参数!\n出错信息:" + ex.Message, ex);
}
try
{
object instance = null;
if (!methodInfo.IsStatic)
instance = Activator.CreateInstance(type, new object[] { });
return methodInfo.Invoke(instance, paramters);
}
catch (Exception ex)
{
throw new Exception("调用方法'" + type.FullName + "." + methodInfo.Name + "'失败\n出错信息:" + ex.Message, ex);
}
}
示例13: ToString
public override string ToString()
{
JObject json = new JObject();
json["type"] = Signable.GetType().Name;
using (MemoryStream ms = new MemoryStream())
using (BinaryWriter writer = new BinaryWriter(ms, Encoding.UTF8))
{
Signable.SerializeUnsigned(writer);
writer.Flush();
json["hex"] = ms.ToArray().ToHexString();
}
JArray multisignatures = new JArray();
for (int i = 0; i < signatures.Length; i++)
{
if (signatures[i] == null)
{
multisignatures.Add(null);
}
else
{
multisignatures.Add(new JObject());
multisignatures[i]["redeem_script"] = signatures[i].redeemScript.ToHexString();
JArray sigs = new JArray();
for (int j = 0; j < signatures[i].signatures.Length; j++)
{
if (signatures[i].signatures[j] == null)
{
sigs.Add(null);
}
else
{
sigs.Add(signatures[i].signatures[j].ToHexString());
}
}
multisignatures[i]["signatures"] = sigs;
}
}
json["multi_signatures"] = multisignatures;
return json.ToString();
}
示例14: ReadOnlyConverterTest
public void ReadOnlyConverterTest()
{
JObject o = new JObject(new JProperty("name", "Hello World"));
string json = o.ToString(Formatting.Indented, new ReadOnlyStringConverter());
StringAssert.Equal(@"{
""name"": ""Hello World""
}", json);
}
示例15: Request
/**
* This is the lowest level request method. Use higher level if possible.
*/
public JObject Request(string uri, string method, ClientType client, JObject parameters, JObject headerOverrides, RequestHandler handler, object state)
{
JObject request = new JObject();
request.Add("parameters", parameters);
JObject header = new JObject();
if (m_token != null)
{
string t = GenerateToken(method);
header.Add("token", t);
}
header.Add("session", m_sid);
if (client == ClientType.HTML)
header.Add("client", "htmlshark");
else if (client == ClientType.JSQueue)
header.Add("client", "jsqueue");
else
throw new Exception("ClientType not supported.");
header.Add("clientRevision", "20101012.37");
header.Add("privacy", 0);
// Somehow this uuid is important, and I don't really know what it is, the UUID of the JSQueue flash object ?
header.Add("uuid", "6BFBFCDE-B44F-4EC5-AF69-76CCC4A2DAD0");
header.Add("country", m_countryObj);
request.Add("header", header);
request.Add("method", method);
if (headerOverrides != null)
{
IDictionaryEnumerator e = headerOverrides.GetEnumerator();
while (e.MoveNext())
{
if (header.ContainsKey(e.Key))
header[e.Key] = e.Value;
else
header.Add(e.Key, e.Value);
}
}
string requestStr = request.ToString().Replace("\n", "").Replace(" ", "").Replace("\r", "");
CookieAwareWebClient wc = new CookieAwareWebClient(m_cc);
wc.UploadStringCompleted += new UploadStringCompletedEventHandler(GSRequestHandler);
wc.UploadStringAsync(new Uri(uri + "?" + method), "POST", requestStr, new object[]{ handler, state });
if(RequestSent != null)
RequestSent(this, requestStr);
return request;
}