首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解析多态JSON数组?

如何解析多态JSON数组?
EN

Stack Overflow用户
提问于 2010-10-03 09:19:13
回答 1查看 17.7K关注 0票数 3

我有一个JSON格式的文件与个人用户的记录。一些用户在他们的记录中间有一个评论字段。我只想解析顶级项目( fullName contributorName电子邮件)

使用Newtonsoft.JSON解析器,但我似乎无法让它识别单个对象。当我将整个字符串解析成一个大对象时,我不知道如何迭代单个对象。

这就是我尝试做的(一个属性接一个属性),但是如果它们是无序的或者有子属性,它就不能工作。我需要把它放到一个对象中:

代码语言:javascript
复制
StreamReader re = File.OpenText("C:\\dropbox\\my dropbox\\clients\\towson\\english 317\\Ning Archive\\ning-members.json");
JsonTextReader reader = new JsonTextReader(re);
string ct = "";

try
{
    ct += "<table style='border:1px solid black'>";
    while (reader.Read())
    {
        if (reader.TokenType == JsonToken.PropertyName)
        {
            if (reader.Value.ToString() == "fullName")
            {
                reader.Read();
                ct += "\r\n\r\n<tr><td>" + reader.Value + "</td>";
            }
            if (reader.Value.ToString() == "contributorName")
            {
                reader.Read();
                ct += "<td>" + reader.Value + "</td></tr>";
            }
            if (reader.Value.ToString() == "email")
            {
                reader.Read();
                ct += "<td>" + reader.Value + "</td>";
            }
        }
    }       
}
catch { }

ct+="</table>";
namesText.Text = ct;

请注意,第一条记录有一个注释字段,我并不关心它,但是当我试图解析为流时,它会妨碍我的顺序。

代码语言:javascript
复制
[
{
    "createdDate": "2010-09-10T14:16:08.271Z",
    "fullName": "Lisa Meloncon",
    "gender": "f",
    "country": "US",
    "birthdate": "1969-05-14",
    "comments": [
        {
            "id": "6292914:Comment:272",
            "contributorName": "0upfj0fd33932",
            "description": "Thanks for joining! I'm working up a schedule for the students a bit late so I can assess some of their early writing (including the first assignment, a general evaluation of business writing skills) and determine a course that will address their needs. I plan to make liberal use of technology this semester, with a Screencasting assignment, some intermediate Word formatting drills, and various other activities.",
            "createdDate": "2010-09-10T18:07:38.272Z"
        }
    ],
    "email": "meloncon@xxx.com",
    "profilePhoto": "http://api.ning.com:80/files/251IcCtIBC3dGALHpG3ruYfg0Ip*EFJApPyMVGkiVArSUEvF*dK8A5grvPvl8eC7i7H0grhRH4pakLc9jSOww2GpU2OTq2nq/626617250.png?crop=1%3A1",
    "level": "member",
    "state": "active",
    "contributorName": "2z42css3dgvoi"
},
{
    "createdDate": "2010-09-08T02:57:00.225Z",
    "fullName": "Robert Calabrese",
    "gender": "m",
    "location": "Baltimore, MD",
    "country": "US",
    "zip": "21284",
    "birthdate": "1989-09-29",
    "email": "rcalab2@xxx.edu",
    "profilePhoto": "http://api.ning.com:80/files/251IcCtIBC3dGALHpG3ruYfg0Ip*EFJApPyMVGkiVArSUEvF*dK8A5grvPvl8eC7i7H0grhRH4pakLc9jSOww2GpU2OTq2nq/626617250.png?crop=1%3A1",
    "level": "member",
    "state": "active",
    "contributorName": "199ru4hzwc4n4"
},
{
    "createdDate": "2010-09-04T22:36:51.158Z",
    "fullName": "Regis Bamba",
    "gender": "m",
    "location": "Baltimore, MD",
    "country": "US",
    "zip": "21210",
    "birthdate": "1986-09-29",
    "email": "rbamba2xxx.edu",
    "profilePhoto": "http://api.ning.com:80/files/251IcCtIBC3dGALHpG3ruYfg0Ip*EFJApPyMVGkiVArSUEvF*dK8A5grvPvl8eC7i7H0grhRH4pakLc9jSOww2GpU2OTq2nq/626617250.png?crop=1%3A1",
    "level": "member",
    "state": "active",
    "contributorName": "2seadgzt89n6x"
},
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-10-03 10:11:56

类似于:

代码语言:javascript
复制
JArray root = JArray.Load(reader);
foreach(JObject o in root)
{
    ct += "\r\n\r\n<tr><td>" + (string)o["fullName"] + "</td>";
    ct += "<td>" + (string)o["contributorName"] + "</td>";
    ct += "<td>" + (string)o["email"] + "</td>";
}

我们使用显式转换从JObject.Item返回的JToken中获取字符串值。

但是,为了提高性能,您应该考虑使用StringBuilder,而不是串联。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3848162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档