如何解析多态JSON数组? [英] How do I parse a polymorphic JSON array?

查看:105
本文介绍了如何解析多态JSON数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON格式的文件,其中包含单个用户的记录.一些用户的记录中间有一个注释字段.我只想解析顶级项目( 全名 贡献者名称 电子邮件)

I have a file in JSON format with record for individual users. Some of the users have a comment field stuck in the middle of their records. I just want to parse top-level items ( fullName contributorName email)

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

using the Newtonsoft.JSON parser, but I can't seem to get it to recognize individual objects. When I parse the whole string into one big object, I don't know how to iterate the individual ones.

这是我尝试执行的操作(按属性划分),但是如果它们出现故障或具有子属性,则无法正常工作.我需要将其放入对象中:

This is how I was trying to do it (property by property), but it didn't work if they were out of order or had subproperties. I need to put it into an object instead:

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;

请注意,第一个记录有一个我不关心的注释字段,但是当我尝试将其解析为流时会妨碍顺序.

Note the first record has a comments field that I don't care about, but gets in the way of the order when I try to parse as a stream.

[
{
    "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"
},

推荐答案

类似的东西

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 .

但是,您应该考虑使用

However, you should consider using StringBuilder, rather than concatenation, for performance.

这篇关于如何解析多态JSON数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆