json 和物件之間的轉換

2021-08-27 12:21:30 字數 2683 閱讀 1400

/// /// list轉成json

///

///

///

///

///

public static string listtojson(ilistlist, string jsonname)

}if (i < list.count - 1)}}

return json.tostring();

}/// /// list轉成json

///

///

///

///

public static string listtojson(ilistlist)

/// /// 物件轉換為json字串

///

/// 物件

/// json字串

public static string tojson(object jsonobject)

stringbuilder value = new stringbuilder();

if (objectvalue is datetime || objectvalue is guid || objectvalue is timespan)

else if (objectvalue is string)

else if (objectvalue is ienumerable)

else

}return jsonstring.tostring().trimend(',') + "}";

}catch (exception ex)

}/// /// 物件集合轉換json

///

/// 集合物件

/// json字串

public static string tojson(ienumerable array)

jsonstring.remove(jsonstring.length - 1, jsonstring.length);

return jsonstring + "]";

}/// /// 普通集合轉換json

///

/// 集合物件

/// json字串

public static string toarraystring(ienumerable array)

jsonstring.remove(jsonstring.length - 1, jsonstring.length);

return jsonstring + "]";

}/// /// datatable轉換為json

///

/// datatable物件

/// json字串

public static string tojson(datatable dt)

else}}

jsonstring.remove(jsonstring.length - 1, 1);

return jsonstring.tostring();

}/// /// datatable轉成json

///

///

///

///

public static string tojson(datatable dt, string jsonname)

}if (i < dt.rows.count - 1)}}

return json.tostring();

}/// /// datareader轉換為json

///

/// datareader物件

/// json字串

public static string tojson(idatareader datareader)

else}}

datareader.close();

jsonstring.remove(jsonstring.length - 1, 1);

if (jsonstring.length == 1)

return jsonstring.tostring();

}/// /// dataset轉換為json

///

/// dataset物件

/// json字串

public static string tojson(dataset dataset)

jsonstring = jsonstring.trimend(',');

return jsonstring + "}";

}/// /// 過濾特殊字元

///

///

///

private static string string2json(string s)

}return sb.tostring();

}/// /// 格式化字元型、日期型、布林型

///

///

///

///

private static string stringformat(string str, type type)

else if (type == typeof(string))

else if (type == typeof(datetime))

else if (type == typeof(bool))

return str;

}

JSON和物件的轉換

object object jsonobject.parse string text jsonobject jsonobject jsonobject.parseobject string text object作為所有類的父類,向上轉型的過程中還是提供了很多方便的。jsonobject跟objec...

Json字串和js物件之間的轉換

var str1 var str2 以 localstorage 為例,sessionstorage 用法一樣 setitem 儲存資料 localstorage.setitem key,value 示例 localstorage.setitem name hello world 當 key 相同時...

Json 與 物件 之間相互轉換

情況一 json 轉 物件 string json this.redisutils.get itemdesc itemid if stringutils.isnoneblank json 情況二 物件 轉 json picuploadresult picuploadresult new picupl...