locked
How to display object name as start when serialize object using dynamic ? RRS feed

  • Question

  • User696604810 posted

    problem

    How to display object name as start when serialize object using dynamic ?

    object name not display as start of json why ?

    generated json I need is 

    request_status : {

    "status" : "failed",

    "code": 1,

    "message" : "login failed, incorrect username or password"

    }

    currently it display as 

    {

    "status" : "failed",

    "code": 1,

    "message" : "login failed, incorrect username or password"

    }

    code used for generate

    dynamic request_status = new JObject();
                    request_status.Status = "failed";
                    request_status.Code = LoginStatus;
                    request_status.Message = errorMessage;
                   
                    JsonResults = JsonConvert.SerializeObject(request_status);

    Saturday, September 7, 2019 4:12 AM

All replies

  • User1289604957 posted

    Hi,

    dynamic request_status = new JObject();
    request_status.Status = "failed";
    request_status.Code = LoginStatus;
    request_status.Message = errorMessage;
    
    var JsonResults = JsonConvert.SerializeObject(request_status, Formatting.Indented);
    Console.WriteLine("request_status : "+JsonResults);

    Best regards,

    Maher

    Saturday, September 7, 2019 10:24 AM
  • User1289604957 posted

    There are two different usages of dynamic (introduced in .NET 4) in .NET. The first <g class="gr_ gr_36 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="36" data-gr-id="36">are</g> .NET properties with a type of dynamic. Dynamic properties behave <g class="gr_ gr_35 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="35" data-gr-id="35">like</g> properties declared as object: any value can be assigned to it, but the difference being that properties and methods can be called on a dynamic property without casting. In Json.NET, dynamic properties are serialized and deserialized exactly the same as untyped objects: because dynamic isn't an actual type, Json.NET falls back to deserializing the JSON as LINQ to JSON objects.

    The second usage of dynamic in .NET <g class="gr_ gr_43 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="43" data-gr-id="43">are</g> by the types that implement IDynamicMetaObjectProvider. This interface lets the implementer create dynamic objects that intercept the property and method calls on an object and use them. ExpandoObject is a good example of a dynamic object.

    Dynamic objects are serialized as JSON objects. A property is written for every member name returned by DynamicMetaObject.GetDynamicMemberNames(). A dynamic object's normal properties aren't serialized by default but can be included by placing the JsonPropertyAttribute on them.

    When deserializing dynamic objects, the serializer first attempts to set JSON property values on a normal .NET member with the matching name. If no .NET member is found with the property name, then the serializer will call <g class="gr_ gr_25 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="25" data-gr-id="25">SetMember</g> on the dynamic object. Because there is no type information for dynamic members on a dynamic object, the values assigned to them will be LINQ to JSON objects.

    Best regards,

    Maher

    Saturday, September 7, 2019 10:25 AM
  • User711641945 posted

    Hi ahmedbarbary,

    You could shape your model the way you want using anonymous classes:

    dynamic request_data = new JObject();
    request_data.Status = "failed";
    request_data.Code = "LoginStatus";
    request_data.Message = "errorMessage";
    var collectionWrapper = new
    {
        request_status = request_data
    };
    var JsonResults = JsonConvert.SerializeObject(collectionWrapper);

    Best Regards,

    Rena

    Monday, September 9, 2019 2:29 AM