Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Form serialize to JSON

function onSubmit( form ){
  var data = JSON.stringify( $(form).serializeArray() ); //  <-----------

  console.log( data );
  return false; //don't submit
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form onsubmit='return onSubmit(this)'>
  <input name='user' placeholder='user'><br>
  <input name='password' type='password' placeholder='password'><br>
  <button type='submit'>Try</button>
</form>
Comment

json serialization

"if youre using System.Text.Json extention"
1. Serialize allows you to convert objects to a json format string:

string defaultPerson = JsonSerializer.Serialize(new Person{
  age = 28,
  nationality = "scottish"
  }) 
  
2. Deserialize allows you to convert json format string to an object:
   ScottishPerson = JsonSerializer.Deserialize<Person>(defaultPerson); 
Comment

java serialize object to json

public class someClass {
//your lcode and logic...
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(MyAwesomeClass.class, new MyAwesomeSerializer());
mapper.registerModule(module);

jsonNode = mapper.convertValue(myAwesomeObject, JsonNode.class);
//... your code and logic
}
  
public class MyAwesomeSerializer extends StdSerializer<MyAwesomeClass> {

    public MyAwesomeSerializer() {
        super(MyAwesomeClass.class);
    }

    @Override
    public void serialize(MyAwesomeClass myAwesomeClass, JsonGenerator jgen, SerializerProvider provider) throws IOException {
        ObjectMapper mapper = new ObjectMapper();

        jgen.writeStartObject();
        jgen.writeStringField("name", myAwesomeClass.getName());
        jgen.writeStringField("age", myAwesomeClass.getAge());

          jgen.writeArrayFieldStart("hobbies");
          for (Hobby hobby : myAwesomeClass.getHobbies()) {
              jgen.writeObject(mapper.convertValue(hobby, JsonNode.class));
          }
          jgen.writeEndArray();
      
        jgen.writeEndObject();
    }

}
Comment

python json serialize object

import json
info = {
    "data": {
        "name": "Dave",
        "City": "NY"
    }
}
# With json.dump  (into file)
with open( "data.json" , "w" ) as x:
    json.dump( info , x )
# >>> {"data": {"name": "Dave", "City": "NY"}}

# with json.dumps (object)
data = json.dumps( info )
print( data )
# >>> {"data": {"name": "Dave", "City": "NY"}}
Comment

serialize object to json

Product product = new Product();
product.ExpiryDate = new DateTime(2008, 12, 28);

JsonSerializer serializer = new JsonSerializer();
serializer.Converters.Add(new JavaScriptDateTimeConverter());
serializer.NullValueHandling = NullValueHandling.Ignore;

using (StreamWriter sw = new StreamWriter(@"c:json.txt"))
using (JsonWriter writer = new JsonTextWriter(sw))
{
    serializer.Serialize(writer, product);
    // {"ExpiryDate":new Date(1230375600000),"Price":0}
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: by value by reference c# 
Csharp :: reflection get enum value C# 
Csharp :: c# linq join mutiple 
Csharp :: save binary data to file c# 
Csharp :: dbset 
Csharp :: c# how to get a securestring from string 
Csharp :: c# how to initialize an array 
Csharp :: Transpose Matrix C# 
Csharp :: camelCase and snakeCase 
Csharp :: how to call a method from a class c# 
Csharp :: string.format() c# 
Csharp :: FiveM pc key code 
Csharp :: access denied tring to save a file uwp xamarin 
Csharp :: C3 compare hour 
Csharp :: RestRequest AdvancedResponseWriter site:stackoverflow.com 
Csharp :: wpf rounded button 
Csharp :: how to solo squad in fortnight 
Csharp :: convert string to float win forms 
Csharp :: c# namespace name form1 could not be found 
Csharp :: how to change argument of function in f# 
Csharp :: join where order by .net framework 
Csharp :: c# sequential struct char array fixed size 
Csharp :: how to check if data already exists in database in c# mvc 
Csharp :: devexpress aspxdatagridview set VerticalScrollableHeight in codebehind 
Csharp :: Zxing Xamarin use front Camera 
Csharp :: [1], [2], [3] 
Csharp :: how to add onclick event dynamically in unity 
Csharp :: C# check control type 
Csharp :: unittest servicector automapper 
Csharp :: c# check if file is zero bytes 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =