admin管理员组文章数量:1321613
I have a simple object that is deserialized from JSON into a server-side object.
JSON:
{
name : 'ObjectName',
host : 'http://localhost',
ImagesPath : '/Images/'
}
On the server side, the above JSON code gets deserialized into this C# object via System.Web.Script.Serialization.JavaScriptSerializer
:
public class InfoObject
{
public string Name { get; set; }
public string Host { get; set; }
public string ImagesPath { get; set; }
}
Currently the above works fine, but I was thinking of adding a lot of properties to it. I want to add sub-objects to hold the extra data so that all the properties are not all in one long class.
Sub-object object:
public class TestSubObject
{
public string TestString { get; set; }
}
So that the new C# object looks like:
public class InfoObject
{
public string Name { get; set; }
public string Host { get; set; }
public string ImagesPath { get; set; }
public TestSubObject MoreInformation {get;set;}
}
But the problem is that I don't know how to represent the initialization of a sub-object property in JSON. Maybe I missed something obvious, but google searches did not immediately yield an answer.
I tried:
{
name : 'ObjectName',
host : 'http://localhost',
ImagesPath : '/Images/',
MoreInformation.TestString : 'hello world'
}
But no dice, so how do I correctly write the above in JSON?
I have a simple object that is deserialized from JSON into a server-side object.
JSON:
{
name : 'ObjectName',
host : 'http://localhost',
ImagesPath : '/Images/'
}
On the server side, the above JSON code gets deserialized into this C# object via System.Web.Script.Serialization.JavaScriptSerializer
:
public class InfoObject
{
public string Name { get; set; }
public string Host { get; set; }
public string ImagesPath { get; set; }
}
Currently the above works fine, but I was thinking of adding a lot of properties to it. I want to add sub-objects to hold the extra data so that all the properties are not all in one long class.
Sub-object object:
public class TestSubObject
{
public string TestString { get; set; }
}
So that the new C# object looks like:
public class InfoObject
{
public string Name { get; set; }
public string Host { get; set; }
public string ImagesPath { get; set; }
public TestSubObject MoreInformation {get;set;}
}
But the problem is that I don't know how to represent the initialization of a sub-object property in JSON. Maybe I missed something obvious, but google searches did not immediately yield an answer.
I tried:
{
name : 'ObjectName',
host : 'http://localhost',
ImagesPath : '/Images/',
MoreInformation.TestString : 'hello world'
}
But no dice, so how do I correctly write the above in JSON?
Share Improve this question edited Mar 24, 2009 at 4:59 Mark Rogers asked Mar 24, 2009 at 1:05 Mark RogersMark Rogers 97.7k19 gold badges90 silver badges141 bronze badges2 Answers
Reset to default 9You can write it like this:
{
Name : 'ObjectName',
Host : 'http://localhost',
ImagesPath : '/Images/',
MoreInformation : {TestString : 'hello world'}
};
// And to access the nested object property:
obj.MoreInformation.TestString
JSON itself is an "object" notation, just put another object inside the "object"
{
key: value,
key2 : { inner_key : value, inner_key2 : value }
}
As you can see, the expression { ... }
yields an object
本文标签: cWhat is the correct syntax to represent subobjects in JSONStack Overflow
版权声明:本文标题:c# - What is the correct syntax to represent subobjects in JSON? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742103046a2420890.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论