Giter VIP home page Giter VIP logo

json-response's Introduction

Json-Response

How to get Json response from Asp.net ( MVC ) to send to Android app or every where that need Json Format of Server Data :)

most of time we need to send data from server in Json format for example for send data from server by Apis to Android app exactly we need Json Format or XML , but i personally use Json most of the time (and i think it's commonly more :) ).

for api coding we can use all server side language like php , asp.net ,... in this article , my goal is Asp.net MVC

in most of databases we have some tables that they are connected to each other (one-to-one , one-to-many , many-to-many) it's mean if we store data in tables that connected to each other , then when fetch one record of that , it has som records that related to itself.

at first imagine we have a table that hasn't any relationship to other tables so if we want to get all data of this in Json format, simply do like bellow

List<Users> users = db.Users.ToList();
return Json(users, JsonRequestBehavior.AllowGet);

now imagin User table has relationship with other tables , our problem exactly start from here !!! why?? because when this line compiled

List<Users> users = db.Users.ToList();

we have all data of users record and ALSO all data that related to each record of User table in other tables !! in some conditions it's good (some times that we want to get special record and all related data) and in some conditions it's too bad (some times that we want just independent data of table without any related data)

now if we use this kind return

return Json(users, JsonRequestBehavior.AllowGet);

then we had an error that said A circular reference was detected while serializing an object of type

so what should we do ? we can do some things based on WHAT WE WANT !!

if we want to fetch special fields of base table so we can do like bellow

public JsonResult test()
    {
      return Json(
             (from u in db.Users select new {
                 name=u.User_Name , family=u.User_Family , location=u.User_location })
            , JsonRequestBehavior.AllowGet);
    }

if we want to fetch special fields of base table and EVEN related tables so we can do like bellow

public JsonResult test()
    {
          return Json(
                     db.Users.Select(u => new {
                                                name = u.User_Name, family = u.User_Family, location=u.User_location
                                               ,gallery = u.Gallery.Select(g => new {
                                                                        galleryName=g.Gallery_Name
                                                                        ,galleryDesc=g.Gallery_Desc
                                           })
                               })
                               , JsonRequestBehavior.AllowGet);
      }

or sth like this

 var gallery = (from u in db.Users 
                        join g in db.Gallery on u.User_ID equals g.Gallery_Admin
                        where u.User_Email == email && g.Gallery_ID == galleryId
                           select new
                        {
                            Gallery_Name = g.Gallery_Name,
                            Gallery_Desc = g.Gallery_Desc,
                            GalleryType = new { GalleryType_ID = g.GalleryType.GalleryType_ID , GalleryType_Title = g.GalleryType.GalleryType_Title, GalleryType_Desc = g.GalleryType.GalleryType_Desc},
                            Gallery_Time = g.Gallery_Time
                        }).FirstOrDefault();

if you want to feth all fields of base table and ALSO all related records in each tables so we can do like bellow

 public ContentResult test()
    {
            var users = db.Users.ToList();

            var list = JsonConvert.SerializeObject(users,
                                                 Formatting.None,
                                  new JsonSerializerSettings()
                                  {
                                      ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                                  });

            return Content(list, "application/json");
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.