none
when generate json object name not shown as start How to do that ? RRS feed

  • Question

  • 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


    What I have tried:

    I need to display as below :
    
    request_status : {
    
    "status" : "failed",
    
    "code": 1,
    
    "message" : "login failed, incorrect username or password"
    
    }
    
    currently it display as idont need 
    
    {
    
    "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:59 AM

Answers

  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer. So that it will help other members to find the solution quickly if they face the similar issue.

    Best Regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 17, 2019 8:03 AM
    Moderator
  • You've received good answers, but I want to make sure you understand why this problem happened.

    request_status is just the name of a variable in your code.  It's not part of the object.  The JSON serializer has no idea what the variable was called.  What he sees is a single object with three members.  Your code would produce exactly the same results even if you replaced "request_status" with "foobar".

    If you want "request_status" to be part of the JSON document, then you have to make it part of the structure you're serializing, as Viorel suggested above.

    HOWEVER, if you really want your display to look exactly as you have posted (which is not valid JSON), then you might consider just printing

        Console.Write( "request_status: ");

    before displaying the JSON.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.



    • Edited by Tim Roberts Tuesday, September 17, 2019 5:21 PM
    • Marked as answer by engahmedbarbary Saturday, September 21, 2019 2:49 PM
    Tuesday, September 17, 2019 5:20 PM

All replies

  • In order to obtain a valid JSON string, try this:

       . . .
       dynamic x =
    new JObject( );

       x.request_status = request_status;

     

       JsonResults = JsonConvert.SerializeObject( x );

     

    The result is:

       {

          "request_status":

          {

             "Status":"failed"

             . . .

          }

       }

     

    But, you can remove the trailing “{ }” using string functions, if this is absolutely necessary.


    Saturday, September 7, 2019 5:31 AM
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer. So that it will help other members to find the solution quickly if they face the similar issue.

    Best Regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 17, 2019 8:03 AM
    Moderator
  • Using below code, you will fix your issue

    private class ClassName { public string field1{ get; set; } public string field2{ get; set; } } var collection = new List<ClassName>(); var collectionData= new { TableName= collection }; var output = JsonConvert.SerializeObject(collectionData);

    final answer will be

    {"TableName":[{"field1":"Elango","field2":"Sengottaiyan"},{...}]}
    Tuesday, September 17, 2019 9:17 AM
  • You've received good answers, but I want to make sure you understand why this problem happened.

    request_status is just the name of a variable in your code.  It's not part of the object.  The JSON serializer has no idea what the variable was called.  What he sees is a single object with three members.  Your code would produce exactly the same results even if you replaced "request_status" with "foobar".

    If you want "request_status" to be part of the JSON document, then you have to make it part of the structure you're serializing, as Viorel suggested above.

    HOWEVER, if you really want your display to look exactly as you have posted (which is not valid JSON), then you might consider just printing

        Console.Write( "request_status: ");

    before displaying the JSON.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.



    • Edited by Tim Roberts Tuesday, September 17, 2019 5:21 PM
    • Marked as answer by engahmedbarbary Saturday, September 21, 2019 2:49 PM
    Tuesday, September 17, 2019 5:20 PM