Categories
Development JSON

How to map JsonConvert jsonString to jsonObject in foreach loop?

How to map data in JsonConvert by I’m using Newtonsoft.Json and use loop foreach data to convert jsonString to jsonObject and I want to get data like the below result that I want to group from jsonObject all. Thank for help.

My parameter :

public class FundInPortRes
{
    public string fcode { get; set; }
    public string abbreviation { get; set; }
    public string chnfname { get; set; }
    public string AMC { get; set; }
    public string chnmain_scope { get; set; }
    public string risklevel { get; set; }
    public List<FundInPortNAVRes> NAV { get; set; }
}

public class FundInPortNAVRes
{
    public string fcode { get; set; }
    public string navdate { get; set; }
    public string nav { get; set; }
    public double navDiff { get; set; }
}

My code :

Array Data F.NAV = [{"fcode":"004123"}, {"nav":"12.83210"}, {"navdate":"20190305"}, {"navDiff":"-0.0351"}]

    [HttpPost]
    public List<Models.FundInPortRes> GetFund2InPort(Models.FundInPortReq model)
    {
        DataAccess.DashDAL dal = new DataAccess.DashDAL();
        List<Models.FundAllRes> models = dal.GetFund2InPort(model);
        List<Models.FundInPortRes> result = new List<Models.FundInPortRes>();

        if (models.Count > 0)
        {
            foreach (var F in models)
            {
                 Models.FundInPortRes resData = new Models.FundInPortRes();
                 resData.fcode = F.fcode;
                 resData.abbreviation = F.abbreviation;
                 resData.chnfname = F.chnfname;
                 resData.AMC = F.AMC;
                 resData.chnmain_scope = F.chnmain_scope;
                 resData.risklevel = F.risklevel;
                 resData.NAV = JsonConvert.DeserializeObject<List<Models.FundInPortNAVRes>>(F.NAV);
                 result.Add(resData);
           }
       }

       return result;
    }

My result :

[{
    "fcode": "004009",
    "abbreviation": "FLEX-RMF",
    "AMC": "ONEONLINE",
    "chnmain_scope": "Fund All",
    "risklevel": "5",
    "NAV": [{
            "fcode": "004009",
            "navdate": null,
            "nav": null,
            "navDiff": 0.0
        },
        {
            "fcode": null,
            "navdate": null,
            "nav": "27.14270",
            "navDiff": 0.0
        },
        {
            "fcode": null,
            "navdate": "20190306",
            "nav": null,
            "navDiff": 0.0
        },
        {
            "fcode": null,
            "navdate": null,
            "nav": null,
            "navDiff": -0.1715
        }
    ]
}]

Result that I want:

[{
    "fcode": "004009",
    "abbreviation": "FLEX-RMF",
    "AMC": "ONEONLINE",
    "chnmain_scope": "Fund All",
    "risklevel": "5",
    "NAV": [{
            "fcode": "004009",
            "navdate": "20190306",
            "nav": "27.14270",
            "navDiff": -0.1715
        }
    ]
}]

Leave a Reply

Your email address will not be published. Required fields are marked *