我如何在asp.net或C#创建JSONARRAY [英] How can I create JSONARRAY in asp.net or c #
本文介绍了我如何在asp.net或C#创建JSONARRAY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要创建并在这个环节的 jsonarray =一样 nofollow的> http://api.learn2crack.com/android/jsonos/ 。我研究,但我找不到例子,我满意。
我知道我是怎么写的 JsonString
,例如:
字符串jsonstring =新System.Web.Script.Serialization.JavaScriptSerializer()序列化(OBJ);
但我在asp.net一个数组,我想写一个数组作为JSONarray这样
{Android的:
[
{版本:1.5,名:蛋糕,API:API级别3},
{版本:1.6,名:甜甜圈,API:API级别4}
]
}
解决方案
为什么我有一种感觉,OP不是一个类型安全的开发商,实际上是问,我怎么写序列化为这个类输出?
公共类Foo
{
公开名单<酒吧GT; Android的{获取;设置;}}
公共类酒吧
{
公共字符串版本{搞定;组; }
公共字符串名称{;组; }
公共字符串API {搞定;组; }
}无功富=新的Foo {
Android的=新名单<酒吧GT;
{
新酒吧{版本=1.5,名称=蛋糕,阿比=API级别3},
新酒吧{版本=1.6,名称=甜甜圈,阿比=API级别4},
}
};
VAR栏=新System.Web.Script.Serialization.JavaScriptSerializer()序列化(富)。
I want to create and write jsonarray
like in this link http://api.learn2crack.com/android/jsonos/. I researched but I could not find example that satisfy me.
I know how I write as JsonString
, for example:
string jsonstring = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(obj);
But I have a array in asp.net and I want to write that array as JSONarray like this
{ "android":
[
{ "ver": "1.5", "name": "Cupcake", "api": "API level 3" },
{ "ver": "1.6", "name": "Donut", "api": "API level 4" }
]
}
解决方案
Why do I have a feeling that the OP isn't a type safe developer and is actually asking, "how do I write a class that serializes to 'this' output?"
public class Foo
{
public List<Bar> Android {get;set;}
}
public class Bar
{
public string Ver { get; set; }
public string Name { get; set; }
public string Api { get; set; }
}
var foo = new Foo{
Android = new List<Bar>
{
new Bar { Ver = "1.5", Name = "Cupcake", Api = "Api Level 3"},
new Bar { Ver = "1.6", Name = "Donut", Api = "Api Level 4"},
}
};
var bar = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(foo);
这篇关于我如何在asp.net或C#创建JSONARRAY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文