Win8开发 如何使用json
如题
我在服务端用了json发到客户端
如何解析!!!!
------解决方案--------------------jons有微软自带的类库,也有官方的类库,都可以解析啊
------解决方案--------------------/*
* 作者:Jina Cookies
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Data;
using System.Collections;
namespace IocUI
{
public class JsonHelper
{
/// <summary>
/// Json2s the obj.
/// </summary>
/// <param name="json">The json.</param>
/// <param name="t">The t.</param>
/// <returns></returns>
public static Object Json2Obj(String json, Type t)
{
try
{
return JsonConvert.DeserializeObject(json, t);
}
catch(Exception ex)
{
return default(Object);
}
}
/// <summary>
/// Json2s the obj.
/// </summary>
/// <param name="json">The json.</param>
/// <param name="t">The t.</param>
/// <returns></returns>
public static TEntity Json2Obj<TEntity>(String json)
{
try
{
return JsonConvert.DeserializeObject<TEntity>(json);
}
catch
{
return default(TEntity);
}
}
/// <summary>
/// Json2s the obj.
/// 只支持单级的Json字符串,不支持复杂的。
/// </summary>
/// <param name="p_json">The p_json.</param>
/// <param name="p_key">The p_data_index.</param>
/// <returns></returns>
public static string Json2Obj(string p_json, string p_key)
{
try
{
var m_json = JArray.Parse(p_json);
return m_json.FirstOrDefault()[p_key].ToString();
}
catch
{
return default(string);
}
}