logo
down
shadow

Serializing Dynamic C# Object as JSON


Serializing Dynamic C# Object as JSON

By : Vlad R
Date : November 18 2020, 11:13 AM
I hope this helps you . I have an ASP.NET MVC app. This app includes C#, Razor, and JSON.NET. I have a C# object that looks like this: , I suggest that you have to do following thing.
Create class.
code :
public class Options
{
    public Options()
    {
    }

    public Options(List<int> d,string approach)
    {
       if (approach == "straight")
       {
          begin = d.Min();
          end = d.Max();
       }
       else if (approach == "curved")
       {
           choosen = d.Select(cc => cc).ToArray();
       }
    }

    public int? begin { get; set; }
    public int? end { get; set; }
    public int[] choosen { get; set; }
}
 var result = (from item in x
                          group item by new { item.Name, item.Approach } into g
                          select new
                          {
                              name = g.Key.Name,
                              approach = g.Key.Approach,
                              options = new Options(g.Select(cc => Convert.ToInt32(cc.Value)).ToList(), g.Key.Approach)
                          }).ToList();

            string value = Newtonsoft.Json.JsonConvert.SerializeObject(result, new Newtonsoft.Json.JsonSerializerSettings() { NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore });


Share : facebook icon twitter icon
Stringify and parse JSON (serializing an object structure) and deserializing, converting a JSON string into an object st

Stringify and parse JSON (serializing an object structure) and deserializing, converting a JSON string into an object st


By : Elawwamy
Date : March 29 2020, 07:55 AM
wish of those help Can anyone guide me how I do this in Javascript? Create an object with two methods. It should stringify and parse JSON, ie it has a method of serializing an object structure, and another method for deserializing, converting a JSON string into an object structure. , Use JSON.parse
code :
var originalObject = { foo: "bar" };
var jsonString = JSON.stringify( originalObject ); // "{foo:'bar'}"
var objectFromString = JSON.parse( jsonString );   // { foo: "bar" }
Json Serializing JDK Dynamic Proxy with Jackson library

Json Serializing JDK Dynamic Proxy with Jackson library


By : Johanna Burns
Date : March 29 2020, 07:55 AM
help you fix your problem You can try Genson library http://code.google.com/p/genson/. I just tested your code with it and it works fine the output is {"name":"foo"}
code :
Planet p = new Planet();
p.setName("foo");
IPlanet ip = getProxy(IPlanet.class, p);
Genson genson = new Genson();
System.out.println(genson.serialize(ip));
Serializing a dynamic property to JSON

Serializing a dynamic property to JSON


By : twinkle
Date : March 29 2020, 07:55 AM
may help you . Your dynamic object has to implement the GetDynamicFieldNames() method properly for dynamic serialization to work.
serializing List with single object as JSON object not JSON array

serializing List with single object as JSON object not JSON array


By : RC.
Date : March 29 2020, 07:55 AM
help you fix your problem If you wanna use your own protocol, you may just return String from method of Spring Controller. I wrote for you a little example. I used "jackson-jaxrs-json-provider) for mapping. You may create you own function for converting your beans. It's very easy.
For example you have a class:
code :
public class MyBean {
    private String name;
    private String value;
    private List<MyBean> beanList = new ArrayList<>();

    public MyBean(String name, String value, MyBean previous) {
        this.name = name;
        this.value = value;
        if (previous != null)
            this.beanList.add(previous);
    }

    //getters & setters
}
public static String convertToMyJson(Object value) {
    try {
        ObjectMapper mapper = new ObjectMapper();
        String resultList = mapper.writeValueAsString(value);
        resultList = "{" + resultList.replaceAll("(^\\[)|(\\]$)", "") + "}";
        System.out.println(resultList);
    }
    catch(Exception e) {
        e.printStackTrace(System.err);
    }
    return "";
}
@RequestMapping(path = "get-bean", method = RequestMethod.GET)
public String getBean() {
    MyBean bean1 = new MyBean("name1", "11", null);
    MyBean bean2 = new MyBean("name2", "22", bean1);
    List<MyBean> beanList = new ArrayList<>();
    beanList.add(bean1);
    beanList.add(bean2);

    return convertToMyJson(beanList);
}
{  
   {  
      "name":"name1",
      "value":"11",
      "beanList":[
      ]
   },
   {  
      "name":"name2",
      "value":"22",
      "beanList":[  
         {  
            "name":"name1",
            "value":"11",
            "beanList":[  
            ]
         }
      ]
   }
}
Serializing nulls and empty Strings in dynamic JSON

Serializing nulls and empty Strings in dynamic JSON


By : Digger Wu
Date : March 29 2020, 07:55 AM
wish help you to fix your issue There are things you might want to try.
First, try using @XmlElement(nillable=true) instead of XmlNullPolicy annotation or setting the emptyNodeRepresentsNull parameter.
Related Posts Related Posts :
  • How to use Addfields in MongoDB C# Aggregation Pipeline
  • MsTest TestCleanup method not called when an unhandled exception is thrown
  • missing last data when exporting gridview to excel
  • How to add array of objects to List in c#
  • Lambda Expression to order (sort) my list collection
  • Library for displaying music notation
  • How to compare two dictionaries in c# and get the output as True and False after validation
  • specify fields to be serialized with JSON
  • How do you obtain the content of a specific node using XmlDocument in C#?
  • How to ignore the first line in a csv file when you read the csv file in C#
  • c# - Problem calling public void from class
  • How to tell a class which objects it should create? Type vs. object confusion :(
  • Save CheckBox state to xml
  • WIX CAQuietExec NETSH Command Fails
  • Issues sending http put request every 60 seconds to RoR app
  • ConfigurationManager.ConnectionStrings.ConnectionString Issue
  • Real size WPF controls for printing
  • How to cancel properly?
  • C# String multiplication error
  • Using Solrnet and Assigning Attributes with Entity Framework Generated POCOs
  • Regex pattern for single backslash
  • TextBox: insert spaces for credit card number?
  • C# DLL loaded for exe-application is not found when launching similar DLL by rundll32.exe
  • Debug a Windows Service with WCF library
  • Open a file from an external assembly?
  • Servicestack RegistrationFeature Unable to bind request
  • Index was outside the bounds of the array confusion
  • Error in Xml to List code. The ':' character, hexadecimal value 0x3A, cannot be included in a name
  • I am trying to do a while loop with a string conditional statement in C#
  • C# 'Cannot access a disposed object. Object name: 'SslStream'.'
  • How to make Gecko use seperate CookieContainer per instance?
  • C# Advanced form "please wait"
  • Send and Receive data C# using network stream
  • How to discover that appsettings changed in C#?
  • Check what needs full trust
  • What is the execution order of an MVC Razor view/layout
  • Table designer (Entity Framework) is too resource intense
  • How to clean up an exception string so it can be displayed via Javascript Alert?
  • Configuring Amazon SES Feedback Notifications via Amazon SNS in ASP.NET MVC (C#)
  • C# api responce and request
  • Dynamodb putitem function not working properly
  • theme in windows phone(light or dark) using c#
  • Backup attached database file(.mdf) using c# and SQL Server
  • What is 'TextFile' and where it is uses in WPF project
  • Using same alias for multiple namespaces
  • how to move mails into separate folders
  • Multithreaded Uploader
  • Memory fragmentation?
  • C# correct exception handling
  • "Could not open macro storage" when accessing using file on another machine
  • How to access other directories of hosted server
  • C# Jagged Array check if value exists/true
  • Why can't I type Clone() properly?
  • exception on accessing dictionary from list
  • Getting the immediate response from server without waiting to 200 message
  • Why am I getting exception Directory Is Not empty?
  • Could not load file or assembly 'CefSharp.dll' or one of its dependencies
  • Sending Email By Using C# in unity3D?
  • Correct usage of await async in webapi
  • Program update code issue
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org