Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Newtonsoft.Json.Linq;
- namespace JSONExample
- {
- public static class JSONTest
- {
- public static JObject ParseJSON(string JSONBuf)
- {
- JObject JSONObj = null;
- try
- {
- JSONObj = JObject.Parse(JSONBuf);
- }
- catch (Exception ex)
- {
- Console.WriteLine("ERROR: " + ex.Message);
- return null;
- }
- return JSONObj;
- }
- public static string GetValueExample(JObject JSONObj, string FieldName)
- {
- string ret = "";
- try
- {
- JToken tok = JSONObj.GetValue(FieldName);
- ret = tok.ToString();
- }
- catch (Exception ex)
- {
- Console.WriteLine("ERROR: " + ex.Message);
- return ret;
- }
- return ret;
- }
- public static string TryGetValueExample(JObject JSONObj, string FieldName)
- {
- string ret = "";
- JToken tok;
- bool result = false;
- result = JSONObj.TryGetValue(FieldName, out tok);
- Console.WriteLine("Result: " + result.ToString());
- if (result)
- {
- ret = tok.ToString();
- }
- else
- {
- Console.WriteLine("Field " + FieldName + " not found!");
- }
- return ret;
- }
- public static string ConstainsKeyExample(JObject JSONObj, string FieldName)
- {
- string ret = "";
- Console.WriteLine("JSONObj.ContainsKey(FieldName): "
- + JSONObj.ContainsKey(FieldName));
- if (JSONObj.ContainsKey(FieldName))
- {
- JToken tok = JSONObj.GetValue(FieldName);
- ret = tok.ToString();
- }
- else
- {
- Console.WriteLine("Field " + FieldName + " not found!");
- }
- return ret;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement