1

I have a controller that access a WCF service which returns a Json object (collection). All rows are of same type, but at different calls the row stricture is different (the return object comes from a user-built sql query, executed with executeReader and serialized as Json So I don't know the row structure upfront.

What I need is an easy way to pass this Json string to something which will generate a view of type list on the fly for it. Doesn't matter formatting, etc, just should be output easily as a table.

Does anyone knows how can I accomplish this?

Another option might be to have something that generate the view on the fly for a IEnumerable of anonymous objects (since using this I could convert the json to a list of anonymous)


EDIT

I found something that does pretty much what I need, except it display metadata about passed object.

It is preetyPrint.js, and I integrated it in my page as below: In my controller I set the result json object to ViewBag.Result, and in the view I used this code:

    <script src="@Url.Content("~/Scripts/prettyprint.js")" type="text/javascript">    </script>
    <div id="resultGrid"></div>
    <script>
    var resultObject = @Html.Raw(ViewBag.Result);            
    var ppTable = prettyPrint(resultObject);
    document.getElementById('resultGrid').appendChild(ppTable);         
    </script>

Does anyone knows such script that actually "dump" the data instead of metadata? Thanks.

1 Answer 1

1

You should create a class to deserialize to if you know the properties of the row. Then use the JavaScriptSerializer class to deserialize to a list of your new class you created. Then you can take a look at the WebGrid class to output the HTML, or just manually iterate over the property metadata in your view.

Creating a custom class will provide you the ability to use metadata to control formatting or other display attributes of the output.

If you cannot create a custom class, you can always use Json.NET or the JavaScriptSerializer to deserialize to a list of dictionary objects or ExpandoObject / Dynamic's or something. Then you would have to manually write something to iterate the keys I think. The ModelMetadataProvider in MVC may be able to handle these allowing you to just iterate the properties in your view code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.