Json Returns additional values ​​from an object

I am using json.net to serlize a generic list, but it returns extra data in my json respose when I only want the field names and values. I have tried using the simple JSONConvert syntax below, but I think I will need to create a new var result based on the value returned from myList.

public string GetListByUserId(int userId)
{

        List<curoList> myList = _db.GetAllListsByUserId(userId);

        var json = JsonConvert.SerializeObject(myList);

        return json;

}

      

In data including things like EntitySetName, when I just want the fields to be as fieldname: fieldvalue and this brings me to my second question from json.net, how would I turn the data into a List object

Edit

I forgot to include test data for guys.

[{"$ ID": "1", "ID": 1, "Name": "David", "LastName": "Buckley", "address1": "," address2 ": zero," district ": zero , "postcode": null, "EMAILADDRESS": null, "aboutme": null, "active": true, "EntityKey": {"$ ID": "2", "EntitySetName": "person", "EntityContainerName ":" curoEntities "," EntityKeyValues ​​": [{" Key ":" identifier "," Type ":" System.Int32 "," Value ":" 1 "}]}}]

Eidt 2 This should show how I am creating my data.

public List<person> GetPersonByIdDal(int personId)
{
        List<person> list = new List<person>();

        try
        {
            list = (from myPersons in curoEntities.persons where myPersons.id == personId && myPersons.active==true select myPersons).ToList();
        }
        catch (Exception ex)
        {
            throw new EntityContextException("GetPersonById where active = true failed.", ex);
        }

        return list;
}

      

+3


source to share


1 answer


The key entities probably come from your ORM. Are you using Entity-Framework? You can use Ignore, or you can do something like

var myList = _db.GetAllListsByUserId(userId).Select(x=> new SomeDto{ prop1 = x.prop1 ... }).toList(); 

public class SomeDto{
    public string prop1 {get;set;}
}

      



This will give you a List object that only contains what you define, and also makes the query faster if you don't want the entire result set to be there.

If you are using Entity framework your db-context can also be configured in dbcontext settings where it can be enabled as proxy creation. Be careful doing this globally as it can mess up your update tracking.

0


source







All Articles