-2

My purpose it to create dynamically objects, I cannot have classes because i do not know the properties. I get from API data about grid fields and columns ("Name":"A", "value": "title 1", "Name":"B", "value": "title 2",). From other call to API i get the data list (string list: ["test 1", "test 2"]) And i am creating JObject like: {"A": "test 1", "B": "Test 2"}. I want to convert it to be like: {A: test 1, b = Test 2} how can i succeed it?

The goal is to have this stucture

public class DataModel
{
    public List<BrowserInfoViewModel> Columns { get; set; }
    public List<object> Data { get; set; }
}

public class BrowserInfoViewModel
{
    public string Header { get; set; }
    public string Name { get; set; };
}

And the data somehow to include information similar to BrowserInfoViewModel Name value.

<DxDataGrid CssClass="mw-1100" @ref="grid" Data="@dataObject.Data">
<Columns>
    @foreach (var column in dataObject.Columns)
    {
        <DxDataGridColumn Caption="@column.Header" Field="@column.Name" Width="400px" />
    }
</Columns>

Where the property of the object must be same as the column.Name.

If the object data is in that structure

{A: test 1, b = Test 2}

The grid binds data and columns succesfully.

But my JObect that casted to Object returns that way and it fails to show data.

{"A": "test 1", "B": "Test 2"} 

Screenshot from datagrid. Screenshot from datagrid

Baskovli
  • 530
  • 5
  • 13
  • 1
    `string val = string.Format("A: {0}, b = {1}", jobj["A"], jobj["B"]);` ? (It would work like [this](https://rextester.com/WPRFD60323)). Is this what you want? – ProgrammingLlama Mar 10 '21 at 08:20
  • Or are you looking for - https://stackoverflow.com/questions/7553516/json-net-serialize-property-name-without-quotes ? – Rand Random Mar 10 '21 at 08:25
  • @John, no this is not what i needed. The result is fine but Datagrid of DevExpress does not accept it as datasource. – Baskovli Mar 10 '21 at 09:08
  • In that case, please can you edit your question so that it's clear what you're trying to achieve. The code I provided converts it to the exact string format you asked for. – ProgrammingLlama Mar 10 '21 at 09:10
  • @John, i have added more information. By the way, thank you for your efford. – Baskovli Mar 10 '21 at 09:37
  • @RandRandom no your suggestion is not what i need. Thank you. – Baskovli Mar 10 '21 at 09:40
  • Perhaps you want to deserialize as `dynamic`? – ProgrammingLlama Mar 10 '21 at 09:55
  • 2
    I believe your claim is incorrect because you are talking about two totally different types, when you say `If the object data is in that structure` `{A: test 1, b = Test 2}` that isn't IMHO a `JSon` object at all that is the .ToString() representation of an anonymous type - https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/anonymous-types - eg. see here https://dotnetfiddle.net/zKydEF so your dataGrid just simply doesn't handle your JSon string or any string for that matter but it wants a anonymous type or any type – Rand Random Mar 10 '21 at 09:55
  • If i could use anonymous type it would be great, my frist thought was that. But is it possible to anonymous type add name like var t = new { dynamicName = "test 1", dynamicName = "Test 2" }; – Baskovli Mar 10 '21 at 10:12
  • The purpose is the name dynamicName to be same as BrowserInfoViewModel.Name in order to library bind the value to the correct columns. – Baskovli Mar 10 '21 at 10:15
  • I don't know how you are parsing your json but it should look something like this https://stackoverflow.com/questions/29074434/convert-from-json-object-to-expando-object-in-c-sharp - I assume your dataGrid will be able to handle dynamic/expandoobject – Rand Random Mar 10 '21 at 10:17
  • I have tried ExpandoObject too. It creates list instead of single item. It binds only the first https://prnt.sc/10hxfa3. Maybe if i create from string instead of JObject the result will be different. – Baskovli Mar 10 '21 at 10:25
  • To help you further you would need to show your JSON and how you convert to expando object, but that is the way to go most likely error is you are trying to convert an array to an object eg. you need `dynamic obj = JsonConvert.DeserializeObject>(jsonObject, expConverter);` but are doing `dynamic obj = JsonConvert.DeserializeObject(jsonObject, expConverter);` or vice versa – Rand Random Mar 10 '21 at 10:33
  • @RandRandom I have created here https://dotnetfiddle.net/kyrvug the method that i am using ExpandoObject, please have a look. – Baskovli Mar 10 '21 at 10:50

1 Answers1

1

(to long for a comment)

Sorry, but I can't see an error with your code in the latest comment https://dotnetfiddle.net/kyrvug

I am not familar with DxDataGrid and Blazor (I assume) but had no issue to put your code into a WPF project which resulted in the desired output.

MainWindow.xaml.cs

(This class also contains your code from https://dotnetfiddle.net/kyrvug since I did no change I ommited it)

public MainWindow()
{
    InitializeComponent();

    var colInfos = new List<BrowserInfoViewModel>()
    {
        new BrowserInfoViewModel() {Header = "Header 1", Name = "Member1"},
        new BrowserInfoViewModel() {Header = "Header 2", Name = "Member2"},
    };
    var rowInfos = new BrowserDataResponseModel()
    {
        Rows = new List<List<string>>()
        {
            new List<string>() {"Member1 - Value 1", "Member2 - Value 1"},
            new List<string>() {"Member1 - Value 2", "Member2 - Value 2"},
            new List<string>() {"Member1 - Value 3", "Member2 - Value 3"},
            new List<string>() {"Member1 - Value 4", "Member2 - Value 4"},
        }
    };

    var dataGridModel = GetValues1212(rowInfos, colInfos);
    MyDataGrid.ItemsSource = dataGridModel.Data;

    foreach (var browserInfoViewModel in dataGridModel.Columns)
        MyDataGrid.Columns.Add(new DataGridTextColumn() { Binding = new Binding(browserInfoViewModel.Name.ToString()), Header = browserInfoViewModel.Header });
}

MainWindow.xaml

<Window x:Class="WpfApp19.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp19"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <DataGrid x:Name="MyDataGrid" AutoGenerateColumns="False" >
    </DataGrid>
</Window>

Result:

Rand Random
  • 7,300
  • 10
  • 40
  • 88
  • Thank you for your help. I finally found the solution which does not has any relevance with the question. With use of Datatable i could manage to bind the data to grid. – Baskovli Mar 10 '21 at 11:50
  • @Baskovli - No problem, glad I could be of help. Congrats on finding a solution that fits your need. – Rand Random Mar 10 '21 at 11:52