Answered by:
JSon deserializer in report

Question
-
User416875641 posted
Hi,
I have a web app built on a framework , its going pretty well.
One issue i have run into is how to deserizale a JSON string made by the app so I can report on it.
It dosen't matter about best practice for this report that is generated, so whatever works, so long as it doesn't impact the rest of the app.
In the report.cshmtl page, I have :
@using Newtonsoft.Json;
The model gets the data, not sure if that is the way to put it. but the values are coming through.
@{
var data =JsonConvert.DeserializeObject<IEnumerable><MyData>>(Model.ModuleName.Image);
}This has two errors
Severity Code Description Project File Line Suppression State Suppression State
Error CS0305 Using the generic type 'IEnumerable<T>' requires 1 type argumentsThe name 'MyData' does not exist in the current context
I tried some making class with @statements, tried making a class next to the file with:
public class MyData
{
public string Filename { get; set; }
public string OriginalName { get; set; }
}So i am trying to get the individual filenames and concat on to folder path and generate value of src for an image.
I alos have another file called report.cs, I guess this is the model.
I tried adding public class myData to it but it didn't work. Still have two errors.
Thursday, October 31, 2019 9:18 AM
Answers
-
User665608656 posted
Hi AltFire,
I am having problem with the myData parameter to the method jsonConvert.What is the problem with the mydata parameter?
I only need the filenames so maybe a straightforward way to do this??Because the original data you provide is a json string, you'd better not to operate directly on it.
According to your previous thinking, you can completely convert JSON string into an array by creating myData class, and then operate the filename field you need in the array.
As you mentioned before, do you want to split the filename field and separate the ModuleName out?
If so , please refer to this code:
string result = @"[{""Filename"":""ModuleName/00001/00001022_bpwcjw4h2p7sy.jpg"",""OriginalName"":""00001022_pap2jxh5645ri_t.jpg""}, { ""Filename"":""ModuleName/00001/00001022_i7mmmqwspc27i.jpg"",""OriginalName"":""demorris -byrd-feQFrMEirNo-unsplash.jpg""}, { ""Filename"":""ModuleName/00001/00001022_ji3gneogkdi6q.jpg"",""OriginalName"":""jason-leung-KFMYmlGUmcM-unsplash.jpg""}, { ""Filename"":""ModuleName/00001/00001022_zq7xe3b7au6jm.jpg"",""OriginalName"":""kevin -mueller-ykrpdbqFMQM-unsplash.jpg""}, { ""Filename"":""ModuleName/00001/00001022_l2q3yrd7chhga.jpg"",""OriginalName"":""david -dvoracek-19-rJG0QENU-unsplash.jpg""}]"; var results = JsonConvert.DeserializeObject<List<MyData>>(result); List<string> imageSrc = new List<string>(); foreach (var item in results) { imageSrc.Add("~/Content/UploadedImages/" + item.Filename.Split('/')[1] + "/" + item.Filename.Split('/')[2]); }
If not, I hope you can explain your needs in detail. What do you need in the end?
Best Regards,
YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 5, 2019 2:13 AM
All replies
-
User665608656 posted
Hi AltFire,
According to your description, I don't know the specific content of Model.ModuleName.Image. Will there be multiple data or only one data after json format conversion?
It's about what type you use to transform it. Please provide us with its details for reference.
I've given two cases here, which you can refer to:
string result = @"[{""Filename"":""AA"",""OriginalName"":""aaaa""},{""Filename"":""BB"",""OriginalName"":""bbbb""}]"; var results = JsonConvert.DeserializeObject<List<MyData>>(result);//with multiple data to return
string result = @"{""Filename"":""AA"",""OriginalName"":""aaaa""}"; var results = JsonConvert.DeserializeObject<MyData>(result); // with one data to return
For more details, you could refer to this link : https://stackoverflow.com/a/10718128
Best Regards,
YongQing.
Friday, November 1, 2019 1:52 AM -
User416875641 posted
Hi YongQing.
Thanks for the reply.
According to your description, I don't know the specific content of Model.ModuleName.Image. Will there be multiple data or only one data after json format conversion?The DB column is like this:
[{"Filename":"ModuleName/00001/00001022_bpwcjw4h2p7sy.jpg","OriginalName":"00001022_pap2jxh5645ri_t.jpg"},
{"Filename":"ModuleName/00001/00001022_i7mmmqwspc27i.jpg","OriginalName":"demorris-byrd-feQFrMEirNo-unsplash.jpg"},
{"Filename":"ModuleName/00001/00001022_ji3gneogkdi6q.jpg","OriginalName":"jason-leung-KFMYmlGUmcM-unsplash.jpg"},
{"Filename":"ModuleName/00001/00001022_zq7xe3b7au6jm.jpg","OriginalName":"kevin-mueller-ykrpdbqFMQM-unsplash.jpg"},
{"Filename":"ModuleName/00001/00001022_l2q3yrd7chhga.jpg","OriginalName":"david-dvoracek-19-rJG0QENU-unsplash.jpg"}]I want to make them individual path names from here, concat with path
"~/Content/UploadedImages/"
. And show on table in report as Img passing the string to <IMG> tag as the "src".
Regards
Friday, November 1, 2019 11:50 AM -
User665608656 posted
Hi AltFire,
According to your description, do you need to combine Filename values or OriginalName values you get with path "~/Content/UploadedImages/" to become the src of <img>?
If so, you can try to convert the json string to the collection form first, then by looping the collection, combine the contents of the columns you need with path, and store src by creating a new list combination.
string result = @"[{""Filename"":""ModuleName/00001/00001022_bpwcjw4h2p7sy.jpg"",""OriginalName"":""00001022_pap2jxh5645ri_t.jpg""}, { ""Filename"":""ModuleName/00001/00001022_i7mmmqwspc27i.jpg"",""OriginalName"":""demorris -byrd-feQFrMEirNo-unsplash.jpg""}, { ""Filename"":""ModuleName/00001/00001022_ji3gneogkdi6q.jpg"",""OriginalName"":""jason-leung-KFMYmlGUmcM-unsplash.jpg""}, { ""Filename"":""ModuleName/00001/00001022_zq7xe3b7au6jm.jpg"",""OriginalName"":""kevin -mueller-ykrpdbqFMQM-unsplash.jpg""}, { ""Filename"":""ModuleName/00001/00001022_l2q3yrd7chhga.jpg"",""OriginalName"":""david -dvoracek-19-rJG0QENU-unsplash.jpg""}]"; var results = JsonConvert.DeserializeObject<List<MyData>>(result); List<string> imageSrc = new List<string>(); foreach (var item in results) { imageSrc.Add("~/Content/UploadedImages/" + item.Filename); }
And is the table in report you mentioned ssrs report? If so , you can refer to this link for reference:
Display Binary Image from database in RDLC Report in ASP.Net using C# and VB.Net
Best Regards,
YongQing.
Monday, November 4, 2019 2:46 AM -
User416875641 posted
Hi,
Sorry the ModuleName is the curent filename, concat with the folder path for the complete path.
Monday, November 4, 2019 10:01 AM -
User416875641 posted
I am having problem with the myData parameter to the method jsonConvert. At the top of the cshtml page I have :
@using Newtonsoft.Json;
@{
List<string> MyData; //List to hold Filenames
string result ;//variable to hold JSON string
}In the body of the page:
@{
result = Model.ModuleName.Image;var results = JsonConvert.DeserializeObject<List<MyData>>(result);
List<string> imageSrc = new List<string>();
foreach (var item in results)
{
imageSrc.Add("~/Content/UploadedImages/" + item.Filename );
}} }
I only need the filenames so maybe a straightforward way to do this??
Monday, November 4, 2019 2:31 PM -
User665608656 posted
Hi AltFire,
I am having problem with the myData parameter to the method jsonConvert.What is the problem with the mydata parameter?
I only need the filenames so maybe a straightforward way to do this??Because the original data you provide is a json string, you'd better not to operate directly on it.
According to your previous thinking, you can completely convert JSON string into an array by creating myData class, and then operate the filename field you need in the array.
As you mentioned before, do you want to split the filename field and separate the ModuleName out?
If so , please refer to this code:
string result = @"[{""Filename"":""ModuleName/00001/00001022_bpwcjw4h2p7sy.jpg"",""OriginalName"":""00001022_pap2jxh5645ri_t.jpg""}, { ""Filename"":""ModuleName/00001/00001022_i7mmmqwspc27i.jpg"",""OriginalName"":""demorris -byrd-feQFrMEirNo-unsplash.jpg""}, { ""Filename"":""ModuleName/00001/00001022_ji3gneogkdi6q.jpg"",""OriginalName"":""jason-leung-KFMYmlGUmcM-unsplash.jpg""}, { ""Filename"":""ModuleName/00001/00001022_zq7xe3b7au6jm.jpg"",""OriginalName"":""kevin -mueller-ykrpdbqFMQM-unsplash.jpg""}, { ""Filename"":""ModuleName/00001/00001022_l2q3yrd7chhga.jpg"",""OriginalName"":""david -dvoracek-19-rJG0QENU-unsplash.jpg""}]"; var results = JsonConvert.DeserializeObject<List<MyData>>(result); List<string> imageSrc = new List<string>(); foreach (var item in results) { imageSrc.Add("~/Content/UploadedImages/" + item.Filename.Split('/')[1] + "/" + item.Filename.Split('/')[2]); }
If not, I hope you can explain your needs in detail. What do you need in the end?
Best Regards,
YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 5, 2019 2:13 AM -
User416875641 posted
AltFire
I am having problem with the myData parameter to the method jsonConvert.I have been trying to grasp this framwork, as it is made with best practice I believe and it allows you to learn what is required to overcome the issues.
But it is a bit mysterous how it works as I only have recently learned about access modifiers and using them in this framework .
I have made a file with these contents:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
namespace ProjectName.ModuleName
{
public class MyData
{
public string FilenameJson { get; set; }
public string OriginalNameJson { get; set; }
}
}var results = JsonConvert.DeserializeObject <List<myData>>(result);
I transformed T4 templates but it still not showing corrrectly, still red!
If I change the myData to a string , the red goes but, it says FileNameJson is not a member of string, as expected.
So , filenameJson is a member of myData, but I can't get it to pull through.
The deserializer needs the list to make a list of myData, itself comproised of two strings(Only really need one).
It is in chtml file so I can't make a public class there.
Wednesday, November 6, 2019 9:27 AM -
User416875641 posted
Ok I done it I had to do:
@using ProjectName.ModuleName.Entities
and put MyData in
namespace project|Name.ModuleName.Entities.
i am getting 3 rows with 3 picture placeholders, when they are 3 pictures on the form.
No images yet.
Thnaks for all you help
Yongqing Yu
Wednesday, November 6, 2019 10:45 AM