c# litjson读取数组如何生成数据数组

[C#技术] .NET平台开源JSON库LitJSON的使用方法
我的图书馆
[C#技术] .NET平台开源JSON库LitJSON的使用方法
String str = "{’name’:’cyf’,’id’:10,’items’:[{’itemid’:1001,’itemname’:’hello’},{’itemid’:1002,’itemname’:’hello2’}]}";& & & & & & &&//*** 读取JSON字符串中的数据 *******************************& & & & & &&JsonData jd = JsonMapper.ToObject(str);& & & & &&String name = (String)jd["name"];&&long id = (long)jd["id"];& & & & & &&JsonData jdItems = jd["items"];& & &&int itemCnt = jdItems.C&// 数组 items 中项的数量&foreach (JsonData item in jdItems)// 遍历数组 items& & & & & &&{int itemID = (int)item["itemid"];& & & & & & & &&String itemName = (String)item["itemname"];& & & &&}& & & & & & &&//*** 将JsonData转换为JSON字符串 ***************************& & & & &&String str2 = jd.ToJson();下载地址 :LitJSON是一个.NET平台下处理的类库,小巧、快速。它的源代码使用C#编写,可以通过任何.Net平台上的语言进行调用,目前最新版本为LitJSON 0.5.0。与以下这几个.Net平台上的开源JSON库相比,LitJSON的性能遥遥领先:&version 0.9.8316&version 0.3.0&Json.NET version 1.1下面介绍LitJSON中常用的方法:以下示例需要先添加引用LitJson.dll,再导入命名空间 using LitJ点击直接下载,也可以到http://litjson.sourceforge.net去下载。&1、Json 与 C#中 实体对象 的相互转换例 1.1:使用 JsonMapper 类实现数据的转换ublic class Person& & {& & & & public string Name { }& & & & public int Age { }& & & & public DateTime Birthday { }& & }& & public class JsonSample& & {& & & & public static void Main()& & & & {& & & & & & PersonToJson();& & & & & & JsonToPerson();& & & & }& & & & ///&& & & & /// 将实体类转换成Json格式& & & & ///&& & & & public static void PersonToJson()& & & & {& & & & & & Person bill = new Person();& & & & & & bill.Name = "";& & & & & & bill.Age = 3;& & & & & & bill.Birthday = new DateTime();& & & & & & string json_bill = JsonMapper.ToJson(bill);& & & & & & Console.WriteLine(json_bill);& & & & & & //输出:{"Name":"","Age":3,"Birthday":"07/17/:00"}& & & & }& & & & ///&& & & & /// 将Json数据转换成实体类& & & & ///&& & & & public static void JsonToPerson()& & & & {& & & & & & string json = @"& & & & & & {& & & & & & & & ""Name""& & : """",& & & & & & & & ""Age""& & & : 3,& & & & & & & & ""Birthday"" : ""07/17/:00""& & & & & & }";& & & & & & Person thomas = JsonMapper.ToObject(json);& & & & & & Console.WriteLine("’87cool’ age: {0}", thomas.Age);& & & & & & //输出:’87cool’ age: 3& & & & }& & }例 1.2:使用 JsonMapper 类将Json字符串转换为C#认识的JsonData,再通过Json数据的属性名或索引获取其值。在C#中读取JsonData对象 和 在的方法完全一样;对Json的这种读取方式在C#中用起来非常爽,同时也很实用,因为现在很多网络应用提供的API所返回的数据都是Json格式的,如Flickr相册API返回的就是json格式的数据。& & & & public static void LoadAlbumData(string json_text)& & & & {& & & & & & JsonData data = JsonMapper.ToObject(json_text);& & & & & & Console.WriteLine("Album’s name: {0}", data["album"]["name"]);& & & & & & string artist = (string)data["album"]["name"];& & & & & & int year = (int)data["album"]["year"];& & & & & & Console.WriteLine("First track: {0}", data["album"]["tracks"][0]);& & & & }2、C# 中对 Json 的 Readers 和 Writers例 2.1:JsonReader类的使用方法&public class DataReader{& & public static void Main ()& & {& & & & string sample = @"{& & & & & & ""name""& : ""Bill"",& & & & & & ""age""& : 32,& & & & & & ""awake"" : true,& & & & & & ""n""& & : ,& & & & & & ""note""& : [ ""life"", ""is"", ""but"", ""a"", ""dream"" ]& & & & & }";& & & & ReadJson (sample);& & }& & //输出所有Json数据的类型和值& & public static void ReadJson (string json)& & {& & & & JsonReader reader = new JsonReader (json);& & & &&& & & & Console.WriteLine ("{0,14} {1,10} {2,16}", "Token", "Value", "Type");& & & & Console.WriteLine (new String (’-’, 42));& & & & while (reader.Read())& & & & {& & & & & & string type = reader.Value != null ? reader.Value.GetType().ToString() : "";& & & & & & Console.WriteLine("{0,14} {1,10} {2,16}", reader.Token, reader.Value, type);& & & & }& & }}& & &&//输出结果://& & & Json类型& & & & 值& & & & & C#类型//------------------------------------------//& ObjectStart& & & & & & & & & & & & & &&//& PropertyName& & & name& & System.String//& & & & String& & & Bill& & System.String//& PropertyName& & & & age& & System.String//& & & & & Int& & & & 32& & System.Int32//& PropertyName& & & awake& & System.String//& & & Boolean& & & True& System.Boolean//& PropertyName& & & & & n& & System.String//& & & & Double& & & System.Double//& PropertyName& & & note& & System.String//& & ArrayStart& & & & & & & & & & & & & &&//& & & & String& & & life& & System.String//& & & & String& & & & is& & System.String//& & & & String& & & & but& & System.String//& & & & String& & & & & a& & System.String//& & & & String& & & dream& & System.String//& & & ArrayEnd& & & & & & & & & & & & & &&//& & ObjectEnd例 2.2:JsonWriter类的使用方法&public class DataReader{& & //通过JsonWriter类创建一个Json对象& & public static void WriteJson ()& & {& & & & System.Text.StringBuilder sb = new System.Text.StringBuilder();& & & & JsonWriter writer = new JsonWriter (sb);& & & & writer.WriteArrayStart ();& & & & writer.Write (1);& & & & writer.Write (2);& & & & writer.Write (3);& & & & writer.WriteObjectStart ();& & & & writer.WritePropertyName ("color");& & & & writer.Write ("blue");& & & & writer.WriteObjectEnd ();& & & & writer.WriteArrayEnd ();& & & & Console.WriteLine (sb.ToString ());& & & & //输出:[1,2,3,{"color":"blue"}]& & }}更详细的可参考 http://litjson.sourceforge.net/doc/manual.html (英文)
TA的最新馆藏
喜欢该文的人也喜欢&>&litjson解析例子
litjson解析例子
上传大小:183KB
使用litJson解析Json数据,例子比较简单,自我感觉使用起来比其它的几个dll要方便许多
综合评分:0(0位用户评分)
下载个数:
{%username%}回复{%com_username%}{%time%}\
/*点击出现回复框*/
$(".respond_btn").on("click", function (e) {
$(this).parents(".rightLi").children(".respond_box").show();
e.stopPropagation();
$(".cancel_res").on("click", function (e) {
$(this).parents(".res_b").siblings(".res_area").val("");
$(this).parents(".respond_box").hide();
e.stopPropagation();
/*删除评论*/
$(".del_comment_c").on("click", function (e) {
var id = $(e.target).attr("id");
$.getJSON('/index.php/comment/do_invalid/' + id,
function (data) {
if (data.succ == 1) {
$(e.target).parents(".conLi").remove();
alert(data.msg);
$(".res_btn").click(function (e) {
var parentWrap = $(this).parents(".respond_box"),
q = parentWrap.find(".form1").serializeArray(),
resStr = $.trim(parentWrap.find(".res_area_r").val());
console.log(q);
//var res_area_r = $.trim($(".res_area_r").val());
if (resStr == '') {
$(".res_text").css({color: "red"});
$.post("/index.php/comment/do_comment_reply/", q,
function (data) {
if (data.succ == 1) {
var $target,
evt = e || window.
$target = $(evt.target || evt.srcElement);
var $dd = $target.parents('dd');
var $wrapReply = $dd.find('.respond_box');
console.log($wrapReply);
//var mess = $(".res_area_r").val();
var mess = resS
var str = str.replace(/{%header%}/g, data.header)
.replace(/{%href%}/g, 'http://' + window.location.host + '/user/' + data.username)
.replace(/{%username%}/g, data.username)
.replace(/{%com_username%}/g, _username)
.replace(/{%time%}/g, data.time)
.replace(/{%id%}/g, data.id)
.replace(/{%mess%}/g, mess);
$dd.after(str);
$(".respond_box").hide();
$(".res_area_r").val("");
$(".res_area").val("");
$wrapReply.hide();
alert(data.msg);
}, "json");
/*删除回复*/
$(".rightLi").on("click",'.del_comment_r', function (e) {
var id = $(e.target).attr("id");
$.getJSON('/index.php/comment/do_comment_del/' + id,
function (data) {
if (data.succ == 1) {
$(e.target).parent().parent().parent().parent().parent().remove();
$(e.target).parents('.res_list').remove()
alert(data.msg);
//填充回复
function KeyP(v) {
var parentWrap = $(v).parents(".respond_box");
parentWrap.find(".res_area_r").val($.trim(parentWrap.find(".res_area").val()));
评论共有0条
上传者:yuruidong1
上传时间:积分/C币:3
上传时间:积分/C币:3
上传者:candycat1992
上传时间:积分/C币:5
上传者:swqgogo
上传时间:积分/C币:3
上传者:cyclone_lnq
上传时间:积分/C币:3
上传者:asd
上传时间:积分/C币:0
上传时间:积分/C币:3
上传者:chenggong2dm
上传时间:积分/C币:3
上传时间:积分/C币:3
上传者:king
上传时间:积分/C币:3
上传者:qq_
上传时间:积分/C币:3
上传者:eoeandroida
上传时间:积分/C币:3
上传者:daydayupn
上传时间:积分/C币:3
上传者:baidu_nod
上传时间:积分/C币:3
上传时间:积分/C币:3
上传者:aqw1w1w1
上传时间:积分/C币:0
上传者:aqi00
上传时间:积分/C币:3
上传者:omayyouhappy
上传时间:积分/C币:3
上传者:ngb5995
上传时间:积分/C币:0
上传者:lovestudy_girl
上传时间:积分/C币:0
审核通过送C币
创建者:zang
创建者:zang
C# winform
上传者其他资源上传者专辑
txt 批量添加尾部信息
pdf文档转word
DOC 批量转 TXT
图片无损放大工具注册版Photozoom_pro7.0.6
织梦采集侠v2.8破解版源码下载
VIP会员动态
CSDN下载频道资源及相关规则调整公告V11.10
下载频道用户反馈专区
下载频道积分规则调整V1710.18
spring mvc+mybatis+mysql+maven+bootstrap 整合实现增删查改简单实例.zip
资源所需积分/C币
当前拥有积分
当前拥有C币
为了良好体验,不建议使用迅雷下载
litjson解析例子
会员到期时间:
剩余下载个数:
剩余C币:593
剩余积分:0
为了良好体验,不建议使用迅雷下载
积分不足!
资源所需积分/C币
当前拥有积分
您可以选择
程序员的必选
绿色安全资源
资源所需积分/C币
当前拥有积分
当前拥有C币
(仅够下载10个资源)
为了良好体验,不建议使用迅雷下载
资源所需积分/C币
当前拥有积分
当前拥有C币
为了良好体验,不建议使用迅雷下载
资源所需积分/C币
当前拥有积分
当前拥有C币
您的积分不足,将扣除 10 C币
为了良好体验,不建议使用迅雷下载
你当前的下载分为234。
你还不是VIP会员
开通VIP会员权限,免积分下载
你下载资源过于频繁,请输入验证码
您因违反CSDN下载频道规则而被锁定帐户,如有疑问,请联络:!
若举报审核通过,可奖励20下载分
被举报人:
举报的资源分:
请选择类型
资源无法下载
资源无法使用
标题与实际内容不符
含有危害国家安全内容
含有反动色情等内容
含广告内容
版权问题,侵犯个人或公司的版权
*详细原因:
litjson解析例子Java.util.ArrayList Class
Java.util.ArrayList Class
Advertisements
Introduction
The java.util.ArrayList class provides resizable-array and implements the List interface.Following are the important points about ArrayList &
It implements all optional list operations and it also permits all elements, includes null.
It provides methods to manipulate the size of the array that is used internally to store the list.
The constant factor is low compared to that for the LinkedList implementation.
Class declaration
Following is the declaration for java.util.ArrayList class &
public class ArrayList&E&
extends AbstractList&E&
implements List&E&, RandomAccess, Cloneable, Serializable
Here &E& represents an Element. For example, if you're building an array list of Integers then you'd initialize it as
ArrayList&Integer& list = new ArrayList&Integer&();
Class constructors
Constructor & Description
ArrayList()
This constructor is used to create an empty list with an initial capacity sufficient to hold 10 elements.
ArrayList(Collection&? extends E& c)
This constructor is used to create a list containing the elements of the specified collection.
ArrayList(int initialCapacity)
This constructor is used to create an empty list with an initial capacity.
Class methods
Method & Description
This method appends the specified element to the end of this list.
This method inserts the specified element at the specified position in this list.
This method appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator
This method inserts all of the elements in the specified collection into this list, starting at the specified position.
This method removes all of the elements from this list.
This method returns a shallow copy of this ArrayList instance.
This method returns true if this list contains the specified element.
This increases the capacity of this ArrayList.
This method returns the element at the specified position in this list.
This method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
This method returns true if this list contains no elements.
This method returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
This method removes the element at the specified position in this list.
This method removes the first occurrence of the specified element from this list, if it is present.
This method removes from this list all of the elements whose index is between fromIndex(inclusive) and toIndex(exclusive).
This method replaces the element at the specified position in this list with the specified element.
This method returns the number of elements in this list.
This method returns an array containing all of the elements in this list in proper sequence (from first to last element).
This method returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.
This method trims the capacity of this ArrayList instance to be the list's current size.
Methods inherited
This class inherits methods from the following classes &
java.util.AbstractList
java.lang.AbstractCollection
java.util.Object
java.util.List
& Copyright 2017. All Rights Reserved.还有很多小孩也没找到,你愿意帮助他们么?15593人阅读
ASP.NET(109)
一个简单示例:
String str = &{’name’:’cyf’,’id’:10,’items’:[{’itemid’:1001,’itemname’:’hello’},{’itemid’:1002,’itemname’:’hello2’}]}&;&&&&&&&&&&&&&
//*** 读取JSON字符串中的数据 *******************************&&&&&&&&&&&
JsonData jd = JsonMapper.ToObject(str);&&&&&&&&&
String name = (String)jd[&name&];&
long id = (long)jd[&id&];&&&&&&&&&&&
JsonData jdItems = jd[&items&];&&&&&
int itemCnt = jdItems.C
// 数组 items 中项的数量
foreach (JsonData item in jdItems)
// 遍历数组 items&&&&&&&&&&&
{int itemID = (int)item[&itemid&];&&&&&&&&&&&&&&&
String itemName = (String)item[&itemname&];&&&&&&&
}&&&&&&&&&&&&&
//*** 将JsonData转换为JSON字符串 ***************************&&&&&&&&&
String str2 = jd.ToJson();
下载地址 :
LitJSON是一个.NET平台下处理的类库,小巧、快速。它的源代码使用C#编写,可以通过任何.Net平台上的语言进行调用,目前最新版本为LitJSON 0.5.0。
与以下这几个.Net平台上的开源JSON库相比,LitJSON的性能遥遥领先:
&version 0.9.8316
&version 0.3.0
&Json.NET version 1.1
下面介绍LitJSON中常用的方法:
以下示例需要先添加引用LitJson.dll,再导入命名空间 using LitJ
点击直接下载,也可以到http://litjson.sourceforge.net去下载。
1、Json 与 C#中 实体对象 的相互转换
例 1.1:使用 JsonMapper 类实现数据的转换
ublic class Person
&&&&&&& public string Name { }
&&&&&&& public int Age { }
&&&&&&& public DateTime Birthday { }
&&& public class JsonSample
&&&&&&& public static void Main()
&&&&&&&&&&& PersonToJson();
&&&&&&&&&&& JsonToPerson();
&&&&&&& ///
&&&&&&& /// 将实体类转换成Json格式
&&&&&&& ///
&&&&&&& public static void PersonToJson()
&&&&&&&&&&& Person bill = new Person();
&&&&&&&&&&& bill.Name = &&;
&&&&&&&&&&& bill.Age = 3;
&&&&&&&&&&& bill.Birthday = new DateTime();
&&&&&&&&&&& string json_bill = JsonMapper.ToJson(bill);
&&&&&&&&&&& Console.WriteLine(json_bill);
&&&&&&&&&&& //输出:{&Name&:&&,&Age&:3,&Birthday&:&07/17/:00&}
&&&&&&& ///
&&&&&&& /// 将Json数据转换成实体类
&&&&&&& ///
&&&&&&& public static void JsonToPerson()
&&&&&&&&&&& string json = @&
&&&&&&&&&&& {
&&&&&&&&&&&&&&& &&Name&&&&& : &&&&,
&&&&&&&&&&&&&&& &&Age&&&&&&& : 3,
&&&&&&&&&&&&&&& &&Birthday&& : &&07/17/:00&&
&&&&&&&&&&& }&;
&&&&&&&&&&& Person thomas = JsonMapper.ToObject(json);
&&&&&&&&&&& Console.WriteLine(&’87cool’ age: {0}&, thomas.Age);
&&&&&&&&&&& //输出:’87cool’ age: 3
例 1.2:使用 JsonMapper 类将Json字符串转换为C#认识的JsonData,再通过Json数据的属性名或索引获取其值。
在C#中读取JsonData对象 和 在的方法完全一样;
对Json的这种读取方式在C#中用起来非常爽,同时也很实用,因为现在很多网络应用提供的API所返回的数据都是Json格式的,
如Flickr相册API返回的就是json格式的数据。
&&&&&&& public static void LoadAlbumData(string json_text)
&&&&&&&&&&& JsonData data = JsonMapper.ToObject(json_text);
&&&&&&&&&&& Console.WriteLine(&Album’s name: {0}&, data[&album&][&name&]);
&&&&&&&&&&& string artist = (string)data[&album&][&name&];
&&&&&&&&&&& int year = (int)data[&album&][&year&];
&&&&&&&&&&& Console.WriteLine(&First track: {0}&, data[&album&][&tracks&][0]);
2、C# 中对 Json 的 Readers 和 Writers
例 2.1:JsonReader类的使用方法&
public class DataReader
&&& public static void Main ()
&&&&&&& string sample = @&{
&&&&&&&&&&& &&name&&& : &&Bill&&,
&&&&&&&&&&& &&age&&& : 32,
&&&&&&&&&&& &&awake&& : true,
&&&&&&&&&&& &&n&&&&& : ,
&&&&&&&&&&& &&note&&& : [ &&life&&, &&is&&, &&but&&, &&a&&, &&dream&& ]
&&&&&&&&& }&;
&&&&&&& ReadJson (sample);
&&& //输出所有Json数据的类型和值
&&& public static void ReadJson (string json)
&&&&&&& JsonReader reader = new JsonReader (json);
&&&&&&& Console.WriteLine (&{0,14} {1,10} {2,16}&, &Token&, &Value&, &Type&);
&&&&&&& Console.WriteLine (new String (’-’, 42));
&&&&&&& while (reader.Read())
&&&&&&&&&&& string type = reader.Value != null ? reader.Value.GetType().ToString() : &&;
&&&&&&&&&&& Console.WriteLine(&{0,14} {1,10} {2,16}&, reader.Token, reader.Value, type);
//输出结果:
//&&&&& Json类型&&&&&&& 值&&&&&&&&& C#类型
//------------------------------------------
//& ObjectStart&&&&&&&&&&&&&&&&&&&&&&&&&&&
//& PropertyName&&&&& name&&& System.String
//&&&&&&& String&&&&& Bill&&& System.String
//& PropertyName&&&&&&& age&&& System.String
//&&&&&&&&& Int&&&&&&& 32&&& System.Int32
//& PropertyName&&&&& awake&&& System.String
//&&&&& Boolean&&&&& True& System.Boolean
//& PropertyName&&&&&&&&& n&&& System.String
//&&&&&&& Double& &&& System.Double
//& PropertyName&&&&& note&&& System.String
//&&& ArrayStart&&&&&&&&&&&&&&&&&&&&&&&&&&&
//&&&&&&& String&&&&& life&&& System.String
//&&&&&&& String&&&&&&& is&&& System.String
//&&&&&&& String&&&&&&& but&&& System.String
//&&&&&&& String&&&&&&&&& a&&& System.String
//&&&&&&& String&&&&& dream&&& System.String
//&&&&& ArrayEnd&&&&&&&&&&&&&&&&&&&&&&&&&&&
//&&& ObjectEnd
例 2.2:JsonWriter类的使用方法&
public class DataReader
&&& //通过JsonWriter类创建一个Json对象
&&& public static void WriteJson ()
&&&&&&& System.Text.StringBuilder sb = new System.Text.StringBuilder();
&&&&&&& JsonWriter writer = new JsonWriter (sb);
&&&&&&& writer.WriteArrayStart ();
&&&&&&& writer.Write (1);
&&&&&&& writer.Write (2);
&&&&&&& writer.Write (3);
&&&&&&& writer.WriteObjectStart ();
&&&&&&& writer.WritePropertyName (&color&);
&&&&&&& writer.Write (&blue&);
&&&&&&& writer.WriteObjectEnd ();
&&&&&&& writer.WriteArrayEnd ();
&&&&&&& Console.WriteLine (sb.ToString ());
&&&&&&& //输出:[1,2,3,{&color&:&blue&}]
更详细的可参考 http://litjson.sourceforge.net/doc/manual.html (英文)
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:2244382次
积分:20701
积分:20701
排名:第427名
原创:324篇
转载:351篇
评论:187条
(10)(11)(1)(3)(1)(4)(4)(5)(11)(1)(1)(2)(2)(1)(7)(11)(8)(7)(1)(4)(3)(6)(16)(13)(3)(12)(13)(52)(12)(20)(6)(18)(45)(13)(28)(4)(15)(7)(42)(26)(13)(24)(36)(22)(10)(9)(5)(17)(7)(11)(26)(10)(13)(15)(10)
(window.slotbydup = window.slotbydup || []).push({
id: '4740887',
container: s,
size: '250,250',
display: 'inlay-fix'

我要回帖

更多关于 litjson json数组 的文章

 

随机推荐