c# json转数组
这种格式的json如何用c#解析[{"ext_value":"当前时间:2019-04-1615:15:29\n当前地点:浙江省台州市大元泵业有限公司","name":"...
这种格式的json 如何用c#解析[{"ext_value":"当前时间:2019-04-16 15:15:29\n当前地点:浙江省台州市大元泵业有限公司","name":"[\"当前时间\",\"当前地点\"]","value":"[\"2019-04-16 15:15:29\",121.257242,28.564215,\"浙江省台州市大元泵业有限公司\",52]"},{"name":"目的厂区","value":"一厂(温岭)"},{"name":"[\"出厂时间\",\"返厂时间\"]","value":"[\"2019-03-21 10:13\",\"2019-03-21 11:17\",\"1.1\"]"},{"name":"出厂目的","value":"品管流程设计"}]
展开
1个回答
展开全部
1.定义个类来对应你的 json 数据:
[DataContract]
public class JsonResult<T>
{
[DataMember]
public string country{ get; set; }
}
2.解析结果:
JsonResultInfo<List<hzJsonCaseApply>> result = (JsonResultInfo<List<hzJsonCaseApply>>)client.ConvertByteDataToObject(你的json数据字节流);
3.相关的方法:
client是自定义的一个类,其下ConvertByteDataToObject方法为:
public object ConvertByteDataToObject(byte[] byteValue)
{
object result = null;
try
{
using (MemoryStream ms = new MemoryStream(byteValue))
{
System.Runtime.Serialization.Json.DataContractJsonSerializer ser = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(object));
result = ser.ReadObject(ms);
}
}
catch (Exception e)
{
result = Activator.CreateInstance(typeof(object));
try
{
System.Reflection.PropertyInfo pi = typeof(object).GetProperty("Message");
if (pi != null)
{
pi.SetValue(result, e.Message, null);
}
}
catch { }
}
return result;
}
[DataContract]
public class JsonResult<T>
{
[DataMember]
public string country{ get; set; }
}
2.解析结果:
JsonResultInfo<List<hzJsonCaseApply>> result = (JsonResultInfo<List<hzJsonCaseApply>>)client.ConvertByteDataToObject(你的json数据字节流);
3.相关的方法:
client是自定义的一个类,其下ConvertByteDataToObject方法为:
public object ConvertByteDataToObject(byte[] byteValue)
{
object result = null;
try
{
using (MemoryStream ms = new MemoryStream(byteValue))
{
System.Runtime.Serialization.Json.DataContractJsonSerializer ser = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(object));
result = ser.ReadObject(ms);
}
}
catch (Exception e)
{
result = Activator.CreateInstance(typeof(object));
try
{
System.Reflection.PropertyInfo pi = typeof(object).GetProperty("Message");
if (pi != null)
{
pi.SetValue(result, e.Message, null);
}
}
catch { }
}
return result;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询