Home > Mobile >  Sending multiple Merged Json Object to webservice and then consume them individually
Sending multiple Merged Json Object to webservice and then consume them individually

Time:12-20

Good Morning Guys! My problem is simple and I need some instant solution. at Client, Side is Creating three different Objects of Json(obj1,obj2,obj3) that I need to send to Backend C# using web Service. As I know we are allowed to send only a single object to web Service so I merge all three independent JSON objects inside another shown below the FinalObject. now after sending the ajax Request can someone tell me who can I get these three objects as individual objects like Obj1, obj2, obj3. at the backend, I have three different Properties set of classes for these three objects. I'm receiving the object call as Object obj. thank you, guys.

 Obj1 = {
            'AlertName': inputs["Alert_Name"].value,
            'Send_to_Admin': ($('#fullAdminUsers').is(':checked') == true?"Yes":"No"),
            'Send_to_Employee': ($('#employee').is(':checked') == true ? "Yes" : "No"),
            'Send_to_Manager': ($('#manager').is(':checked') == true ? "Yes" : "No"),
            'Send_to_Others': ($('#otherSendTo').is(':checked') == true ? "Yes" : "No"),
            'EmailSubject': inputs['EmailSubject'].value,
            'CustomMessage': inputs['CustomMessage'].value,
            
        }
    Obj2 = {
             "EmployeeId": id,
             "Employee_Name": emp_name,
             "Alert_Group_Id": Is_allEmployees
        }
    Obj3 = {
            newObject[key]["EventScope"] = EventScope;
            newObject[key]["EventName"] = EventName;
            newObject[key]["Eventtype"] = Eventtype;
        }
     FinalObject = {
            'AlertData': Obj1,
            'AlertGroup': Obj2,
            'ERemainder': Obj3
        }

CodePudding user response:

you can try that. Just convert this daha to JObject, then you can achieve to nested objects like data["AlertData"]. Then cast it whatever you want.

var obj1 = new { key1 = "value1" };
        var obj2 = new { key2 = "value2" };
        var obj3 = new { key1 = "value3" };

        var finalObj = new { AlertData = obj1, AlertGroup = obj2, ERemainder = obj3 };

        var jsonData = JsonConvert.SerializeObject(finalObj);

        var jData = (JObject)JsonConvert.DeserializeObject(jsonData.ToString());

        var alertData = (dynamic)jData["AlertData"];

I used dynamic because i dont know what you want. You can use your real type instead of dynamic. You can achieve to Values like alertData.key in this scenario.

  • Related