locked
Return JSON from DB in a Custom Format RRS feed

  • Question

  • User1122679459 posted

    Hi,

    I'm using <g class="gr_ gr_17 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="17" data-gr-id="17">Json</g>.Write() to get <g class="gr_ gr_65 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="65" data-gr-id="65"><g class="gr_ gr_106 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="106" data-gr-id="106">db</g></g> result as <g class="gr_ gr_93 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="93" data-gr-id="93">json</g> but I need it in this format:

    Here is my code:

    var results = db.Query(sql, "%" + query + "%");
    var data = results.Select(p => new { name = p.Company, logo = p.LogoUrl, url = p.RouteUrl, Discount = p.Discount });
    Json.Write(data, Response.Output);

    This is the result I need. How can I get results with "suggestions" and "data" with children?

    {
            "suggestions":[
                {"value":"Dell","data":{"logoUrl":"logo.png", "RouteUrl":"some path", "Discount":"1%" }},
                {"value":"Walmart","data":{"LogoUrl":"logo.png", "RouteUrl":"some path", "Discount":"5%" }}
            ]
        }

    Wednesday, July 25, 2018 2:38 PM

Answers

  • User475983607 posted

    You're not following standard practises. 

    First create a C# class that has the expected format.  All you have to do is copy the expected JSON format then paste it into a cs file in Visual Studio.  Click Edit -> Paste Special -> Paste as JSON class.  You'll need to fix the LogoUrl case as you have a typo in the JSON file.

        public class Rootobject
    {
    public Suggestion[] suggestions { get; set; }
    }

    public class Suggestion { public string value { get; set; } public Data data { get; set; } } public class Data { public string logoUrl { get; set; } public string RouteUrl { get; set; } public string Discount { get; set; } }

    Next, fill the class using your data access layer.   The common approach is using an EF project query.  I can't see your DB schema so I can't provide accurate code to populate the class.  You'll need to figure that bit out on your own.

    Once you have the class populated simply return the class just like any C# method.  The following is an example uses Web API.  I hardcoded the values to simulate fill the class with data from the data access layer.

    public Rootobject Get()
    {
        Rootobject root = new Rootobject()
        {
            suggestions = new Suggestion[]
            {
                new Suggestion()
                {
                    value = "Dell",
                    data = new Data()
                    {
                        Discount = "1%",
                        logoUrl = "logo.png",
                            RouteUrl = "some path"
                    }
                },
                new Suggestion()
                {
                    value = "Walmart",
                    data = new Data()
                    {
                        Discount = "5%",
                        logoUrl = "logo.png",
                            RouteUrl = "some path"
                    }
                }
            }
        };
    
        return root;
    }

    I recommend that you go through a few of the Getting Started tutorials located on this site from the "Learn" link above.  You'll need to pick the technology you're using.

    https://www.asp.net/web-api

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 25, 2018 3:51 PM