本文目录一览:
C# 怎么将json 转换成 datatable
主要包含两个方法:
1. 获取的DataTable 对象 转换为Json 字符串
2. Json 字符串 转换为 DataTable数据集合
#region DataTable 转换为Json字符串实例方法
/// summary
/// GetClassTypeJosn 的摘要说明
/// /summary
public class GetClassTypeJosn : IHttpHandler
{
/// summary
/// 文件名:DataTable 和Json 字符串互转
/// 版权所有:Copyright (C) Create Family Wealth liangjw
/// 创建标示:2013-08-03
/// /summary
//用法说明实例
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
context.Response.Charset = "utf-8";
HttpRequest req = context.Request;
string method = req["method"].ToStr().ToLower();
//获取合同明细列表 DataTable 转换为Json字符串
if (method == "txtdate")
{
string json = "";
BO.MakeContractMx bll = new MakeContractMx();
DataSet ds = bll.GetDataTable();
if (ds.Tables.Count 0)
{
json =ToJson(ds.Tables[0]);
}
context.Response.Write(json);
return;
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
#endregion
#region Json字符串转换为DataTable 实例方法
public DataTable JsonToDataTable(json)
{
DataTable dt= ToDataTable(json);
return dt;
}
#endregion
#region DataTable 转换为Json 字符串
/// summary
/// DataTable 对象 转换为Json 字符串
/// /summary
/// param name="dt"/param
/// returns/returns
public static string ToJson(this DataTable dt)
{
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = new ArrayList();
foreach (DataRow dataRow in dt.Rows)
{
Dictionarystring, object dictionary = new Dictionarystring, object(); //实例化一个参数集合
foreach (DataColumn dataColumn in dt.Columns)
{
dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToStr());
}
arrayList.Add(dictionary); //ArrayList集合中添加键值
}
return javaScriptSerializer.Serialize(arrayList); //返回一个json字符串
}
#endregion
#region Json 字符串 转换为 DataTable数据集合
/// summary
/// Json 字符串 转换为 DataTable数据集合
/// /summary
/// param name="json"/param
/// returns/returns
public static DataTable ToDataTable(this string json)
{
DataTable dataTable = new DataTable(); //实例化
DataTable result;
try
{
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = javaScriptSerializer.DeserializeArrayList(json);
if (arrayList.Count 0)
{
foreach (Dictionarystring, object dictionary in arrayList)
{
if (dictionary.Keys.Countstring() == 0)
{
result = dataTable;
return result;
}
if (dataTable.Columns.Count == 0)
{
foreach (string current in dictionary.Keys)
{
dataTable.Columns.Add(current, dictionary[current].GetType());
}
}
DataRow dataRow = dataTable.NewRow();
foreach (string current in dictionary.Keys)
{
dataRow[current] = dictionary[current];
}
dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
}
}
}
catch
{
}
result = dataTable;
return result;
}
#endregion
#region 转换为string字符串类型
/// summary
/// 转换为string字符串类型
/// /summary
/// param name="s"获取需要转换的值/param
/// param name="format"需要格式化的位数/param
/// returns返回一个新的字符串/returns
public static string ToStr(this object s, string format = "")
{
string result = "";
try
{
if (format == "")
{
result = s.ToString();
}
else
{
result = string.Format("{0:" + format + "}", s);
}
}
catch
{
}
return result;
}
#endregion
Java将Json字符串转成对象的问题
用Gson。下载地址:
定义Java类:
class Feature {
public Attributes attributes;
public Geometry geometry;
}
class Attributes {
public String OBJECTID, ID, CD, HG, DP_AS.......;
}
class Geometry {
public Float x, y;
}
Feature feature;
public static MyObject fromJson(String json) {
return new Gson().fromJson(json, MuObject.class);
}
}
然后在需要的地方调用:
MyObject obj = MyObject.fromJson("{......}");
MyObject.Attributes attributes = obj.feature.attributes;
MyObject.Geometry geometry = obj.feature.geometry;
json 这个格式怎么获取data值
下载一个 Newtonsoft.Json.dll 插件
然后给你贴一个我做过的方法 自己改改
C# code
?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.IO;
using System.Web.Script.Serialization;
using System.Web;
namespace MicroBlog.Common.APIHelper
{
public class Resolve
{
/// summary
/// JSON文本转对象,泛型方法
/// /summary
/// typeparam name="T"类型/typeparam
/// param name="jsonText"JSON文本/param
/// returns指定类型的对象/returns
public static T JSONToObjectT(string jsonText, HttpContext context)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
try
{
return jss.DeserializeT(jsonText);
}
catch (Exception ex)
{
//
}
}
/// summary
/// 将JSON文本转换成数据行
/// /summary
/// param name="jsonText"JSON文本/param
/// returns数据行的字典/returns
public static Dictionarystring, object DataRowFromJSON(string jsonText, HttpContext context)
{
return JSONToObjectDictionarystring, object(jsonText, context);
}
}
}
json中包含json数组怎么转换成java对象
下面是JSON转pojo的工具类,复制就可以用了。
import java.util.List;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* 淘淘商城自定义响应结构
*/
public class JsonUtils {
// 定义jackson对象
private static final ObjectMapper MAPPER = new ObjectMapper();
/**
* 将对象转换成json字符串。
*/
public static String objectToJson(Object data) {
try {
return MAPPER.writeValueAsString(data);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
/**
* 将json结果集转化为对象
*
* @param jsonData
* json数据
* @param clazz
* 对象中的object类型
* @return
*/
public static T T jsonToPojo(String jsonData, ClassT beanType) {
try {
T t = MAPPER.readValue(jsonData, beanType);
return t;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将json数据转换成pojo对象list
*/
public static T ListT jsonToList(String jsonData, ClassT beanType) {
JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
try {
ListT list = MAPPER.readValue(jsonData, javaType);
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}