Advertisement
Guest User

Untitled

a guest
May 30th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9. using Newtonsoft.Json;
  10. using Newtonsoft.Json.Linq;
  11.  
  12. class Program
  13. {
  14. static void Main(string[] args)
  15. {
  16. string jsonstr =
  17. "{\"timestamp\":\"2016-04-08 11:21:25\",\"apiKey\":\"jdtest\",\"data\":{\"adultQuantity\":3,\"childQuantity\":0,\"babyQuantity\":0,\"segmentList\":[{\"departCityCode\":\"PNZ\",\"arriveCityCode\":\"SHA\",\"departDate\":\"2016-04-19\"}]},\"H\":\"value\",\"c\":null,\"v\":\"\",\"w\":\"null\"}";
  18. Dictionary<string, object> dict = new Dictionary<string, object>();
  19.  
  20. JObject _jObject = JObject.Parse(jsonstr);
  21.  
  22. string str = Fun(_jObject);
  23. }
  24.  
  25. public static string Fun(JObject obj)
  26. {
  27. string result = null;
  28.  
  29. foreach (var item in obj)
  30. {
  31. if (typeof(JObject) == item.Value.GetType())
  32. {
  33. JObject child = (JObject)item.Value;
  34. string tmp = Fun(child);
  35. result += tmp;
  36. }
  37. else if (typeof(JArray) == item.Value.GetType())
  38. {
  39. JArray _jarray = (JArray)item.Value;
  40. foreach (var jitem in _jarray)
  41. {
  42. JObject jchild = (JObject)jitem;
  43. string tmp = Fun(jchild);
  44. result += tmp;
  45. }
  46. }
  47. else
  48. {
  49. if (!(item.Value != null && item.Value.ToString().Trim().Equals("")))
  50. {
  51. result += string.Format("{0}={1},", item.Key, item.Value);
  52. }
  53. }
  54. }
  55. return result;
  56. }
  57.  
  58. public static T JsonDeSerializerObj<T>(string json)
  59. {
  60. T t = JsonConvert.DeserializeObject<T>(json);
  61. return t;
  62. }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement