remove attribute in json that contain multivalue in asp.net c# before show it in gridview

317 views Asked by At

I'm newbie in asp.net c# . Iwant to show the json data into table with gridview .I have json data with this format :

[{
    "reviewerID": "A1YS9MDZP93857",
    "asin": "0006428320",
    "reviewerName": "John Taylor",
    "helpful": [
        0,
        0
    ],
    "reviewText": "last movement of sonata #6 is missing. What should one expect?",
    "overall": 3,
    "summary": "Parts missing",
    "unixReviewTime": 1394496000,
    "reviewTime": "03 11, 2014"
},
{
    "reviewerID": "A3TS466QBAWB9D",
    "asin": "0014072149",
    "reviewerName": "Silver Pencil",
    "helpful": [
        0,
        0
    ],
    "reviewText": "If you are a serious violin student on a budget, this edition has it all",
    "overall": 5,
    "summary": "Perform it with a friend, today!",
    "unixReviewTime": 1370476800,
    "reviewTime": "06 6, 2013"
}]

to show that into gridview I use the following code (using Json.net library)

JsonString = TextBox1.Text;
       dt = (DataTable)JsonConvert.DeserializeObject(JsonString, (typeof(DataTable)));
       GridView1.DataSource = dt;
       GridView1.DataBind();

the problem is gridview cannot show the data, and it's work if I remove "helpfull" attribute like this one by one :

{
"reviewerID": "A1YS9MDZP93857",
"asin": "0006428320",
"reviewerName": "John Taylor",
"reviewText": "The portfolio is fine except for the fact that the last movement of sonata #6 is missing. What should one expect?",
"overall": 3,
"summary": "Parts missing",
"unixReviewTime": 1394496000,
"reviewTime": "03 11, 2014"}

I don't know code how to remove it , Since i have large json data, i'ts difficult and wasting time to remove it manually. Any idea?

1

There are 1 answers

11
dbc On BEST ANSWER

As you are using Json.NET, if you want to remove unwanted properties before deserializing to DataTable, you can convert to an intermediate LINQ to JSON JToken hierarchical representation, then remove properties with unwanted names. After this is done, you can deserialize to your final DataTable class with JToken.ToObject<T>.

To parse to a JToken, do:

        var obj = JToken.Parse(JsonString);

Then to remove unwanted properties by name:

        var unwanteds = new HashSet<string>(new[] 
        {
            "helpful"
        });
        foreach (var property in obj.Children().SelectMany(o => o.OfType<JProperty>()).Where(p => unwanteds.Contains(p.Name)).ToList())
        {
            property.Remove();
        }

Or, to remove all except wanted properties, by name:

        var keepers = new HashSet<string>(new[]
        {
            "reviewerID",
            "asin",
            "reviewerName",
            "reviewText",
            "overall",
            "summary",
            "unixReviewTime",
            "reviewTime",
        });
        foreach (var property in obj.Children().SelectMany(o => o.OfType<JProperty>()).Where(p => !keepers.Contains(p.Name)).ToList())
        {
            property.Remove();
        }

Finally, deserialize:

        var dt = obj.ToObject<DataTable>();