我有一个用户数组,我想作为一个javascript数组传递给一个视图.我是通过JavaScriptSerializer做的,但我得到的字符串中没有未转义的引号.
控制器动作
public virtual ActionResult Create(int id){
var viewModel = new AttendeeViewModel();
var data = viewModel.GetMembershipUserList() ;
viewModel.MeetingID = id;
viewModel.Users = new JavaScriptSerializer().Serialize(data.ToArray());
return View(viewModel);
}
Run Code Online (Sandbox Code Playgroud)
视图
<script type="text/javascript">
var tags = @Model.Users
$(document).ready(function () { $("#mytags").tagit({ availableTags:tags}); });
</script>
Run Code Online (Sandbox Code Playgroud)
视图模型
public class AttendeeViewModel
{
public int AttendeeId { get; set; }
public string Name { get; set; }
public int MeetingID { get; set; }
public string Users { get; set; }
public List<string> GetMembershipUserList()
{
List<string> userNames …Run Code Online (Sandbox Code Playgroud) 显然,IDictionary<string,object>被序列化为KeyValuePair对象数组(例如[{Key:"foo", Value:"bar"}, ...]).是否可以将其序列化为对象(例如{foo:"bar"})?
我正在寻找一个示例代码/ lib来使用C#解码JSON字符串.
编码我可以这样做:
var data = new Dictionary<string,string>();
data.Add("..", "...");
var json_encoded = new JavaScriptSerializer().Serialize(data);
Run Code Online (Sandbox Code Playgroud)
但我怎么解码?
var json_decoded = ??
Run Code Online (Sandbox Code Playgroud) 我有一些JSON,我需要反序列化,所以我使用JavaScriptSerializer.DeserializeObject,如:
var jsonObject = serializer.DeserializeObject(line) as Dictionary<string, object>;
Run Code Online (Sandbox Code Playgroud)
问题是返回的Dictionary有一个区分大小写的密钥比较器,但我需要不区分大小写.有没有办法找回不区分大小写的Dictionary?
编辑:我不想将数据复制到新的结构,因为我有很多数据,这将是昂贵的.
我在powershell 2.0中编写脚本,目前无法升级到3.0或更高版本.在这个脚本中,我尝试使用此链接中的代码将一些数据序列化为JSON(PowerShell 2.0 ConvertFrom-Json和ConvertTo-Json实现):
function ConvertTo-Json20([object] $item){
add-type -assembly system.web.extensions
$ps_js=new-object system.web.script.serialization.javascriptSerializer
return $ps_js.Serialize($item)
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我以某种方式获得循环引用,我真的不知道为什么.我设置了一小段测试数据,结构在powershell中看起来像这样:
$testRoot = @{
"id" = "1"
"children" = @(
@{
"id" = "2"
"children" = @(
@{
"id" = "2";
};
@{
"id" = "3";
}
);
};
@{
"id" = "4"
"children" = @(
@{
"id" = "5";
}
);
}
)
}
Run Code Online (Sandbox Code Playgroud)
我知道它看起来很垃圾,但我只需要这种格式.
我需要序列化的结构有更多的层,所以更多的"孩子",并有它变得奇怪的点.
当我尝试这个:
ConvertTo-Json20 $testRoot
Run Code Online (Sandbox Code Playgroud)
一切正常.结构被解析如下:
{
"id":"1",
"children":[
{
"id":"2",
"children":[
{
"id":"2"
},
{
"id":"3"
} …Run Code Online (Sandbox Code Playgroud) 我有一个js对象结构如下:
object.property1 = "some string";
object.property2 = "some string";
object.property3.property1 = "some string";
object.property3.property2 = "some string";
object.property3.property2 = "some string";
Run Code Online (Sandbox Code Playgroud)
我正在使用JSON.stringify(object)通过ajax请求传递它.当我尝试使用JavaScriptSerializer.Deserialize作为字典反序列化时,我得到以下错误:
没有为'System.String'类型定义无参数构造函数.
这个完全相同的过程适用于具有非"集合"属性的常规对象..感谢您的帮助!
试图反序列化这个JSON:
{
"result":"success"
"arguments": {
"activeTorrentCount":22,
"cumulative-stats": {
"downloadedBytes":1111,
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的课:
private class DeserializationMain
{
public string result; //works
public args arguments; //works, has deserialized activeTorrentCount
public class args
{
public int activeTorrentCount;
public current cumulative_stats; //doesn't work, equals null
public class current
{
public long downloadedBytes;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想累积统计数据不会被反序列化,因为它在我的类中有cumulative_stats变量名,如何用破折号反序列化那个东西?
根据迭戈在这个问题中得票最高的答案下未答复的评论:
所以对于一个枚举:
public enum ContactType
{
Phone = 0,
Email = 1,
Mobile = 2
}
Run Code Online (Sandbox Code Playgroud)
例如。一个属性:
//could contain ContactType.Phone, ContactType.Email, ContactType.Mobile
IEnumerable<ContactType> AvailableContactTypes {get;set;}
Run Code Online (Sandbox Code Playgroud)
对于类似 JSON 的内容:
{ContactTypes : ['Phone','Email','Mobile']}
Run Code Online (Sandbox Code Playgroud)
代替
{ContactTypes : [0,1,2]}
Run Code Online (Sandbox Code Playgroud)
与普通 JavaScriptSerializer 的情况一样吗?
为什么以下代码返回"[]"时应返回"{"id":1999,"title":"hithere"}
JavaScriptSerializer serializer = new JavaScriptSerializer();
StringBuilder sbJsonResults = new StringBuilder();
var result = serializer.Serialize(new dg(1999, "hithere"));
context.Response.Clear();
context.Response.ContentType = "application/json; charset=utf-8";
context.Response.Cache.SetExpires(DateTime.MinValue);
context.Response.Write(result);
Run Code Online (Sandbox Code Playgroud)
PS dg类看起来像这样:
public class dg : ScheduleObserver, ILibrary, IEnumerable {
public int id;
public string title;
private List<d> dList;
...many getters and setters and some logic functions...
}
public abstract class ScheduleObserver{
public abstract void update();
}
public interface ILibrary {
List<PD> getPDs();
void setPDs(List<PD> newPDs);
int getCurrentIndex();
void addPD(PD pD);
PD getPD(int index);
}
Run Code Online (Sandbox Code Playgroud)
非常感谢. …
我正在使用JavaScriptSerializer来反序列化json数据.一切都很好,但我的问题是,json数据中的一个属性被命名为'base',所以我不能在我的C#代码中创建这样的属性.我发现我可以手动将值映射到构造函数中的属性,但问题是,我的DTO有200个属性,所以我不想手动创建它,而是希望找到任何其他解决方案.我也尝试使用注释,但是这个:
[JsonProperty("base")]
public int baseValue { get; set; }
Run Code Online (Sandbox Code Playgroud)
没有帮助我,值baseValue每次设置为0(如果你认为,这个注释应该工作,我可以发布我的整个代码,不仅这2行)
有什么办法可以简单地解决我的问题吗?
json ×7
c# ×6
javascript ×3
.net ×2
asp.net-ajax ×1
asp.net-mvc ×1
jquery ×1
json.net ×1
powershell ×1