How can I create Dictionary Type of Object from JSON type of string - string

I am looking to create a dictionary type of object from below string without using any extension class. I would prefer to write a .net class which will do serialize and deserialize it.
string userDetails = "{"FullName":"Manoj Singh","username":"Manoj","Miles":2220,"TierStatus":"Gold","TierMiles":23230,"MilesExpiry":12223,"ExpiryDate":"31 January 2011","PersonID":232323,"AccessToken":"sfs23232s232","ActiveCardNo":"232323223"}";
I have got above string in my results, now I want to convert it into dictionary type of Object using .NET 2.0.
Thanks.

I was waiting since many days to have any response on it, unfortunately there were no response, well guys I resolved my problem with the help of this question Can you Instantiate an Object Instance from JSON in .NET?
Thanks to all you who have seen this question!

try this!!
Class deserialize<Class>(string jsonStr, Class obj) { /* ... */}
var jstring = "{fname='Test', lname='test, oth='test'}";
var p = deserialize(jstring, new {fname="",lname=""});
var x = person.fname; //strongly-typed

Related

Groovy - How to serialize an object into a string

How to serialize an object into a string
below is the .net code for serializing an object into a string
String sampleEntity= JsonConvert.SerializeObject(entity))
same I need it in groovy? please suggest
Assuming entity is some object or list of objects, the easiest way IMO is:
import groovy.json.*
class Person { // this is a sample object, like entity in your example
String name
}
def json = JsonOutput.toJson([ new Person(name: 'John'), new Person(name: 'Max') ])
println json​
// output (string): [{"name":"John"},{"name":"Max"}]
If you need to customize the output (like fiddle with exact format of dates or something), you should use JsonGenerator Instead. It has a builder that will allow to do this fine grained setup. Since its a kind of beyond the scope of the question, I'll just provide a link to the relevant chapter of documentation

Haxe - use string as variable name with DynamicAccess

I am trying to use a string ('npcName') as a variable name. So far I have tried casting dialogMap into a DynamicAccess object, but it gives me the error 'Invalid array access' when I try this:
var npcName:String = 'TestNPC';
var casted = (cast Registry.dialogMap:haxe.DynamicAccess<Dynamic>);
var tempname = casted[root.npcName[0].message];
trace(tempname);
'dialogMap' is an empty map which I want to fill like so:
Registry.dialogMap['message'] = root.npcName[0].message;
How can I use npcName, a string, in the above line of code? Is there a way to transform the string into something usable? Any help would be appreciated.
The haxe.DynamicAccess doesn't have array access (like map[key]), but is an abstract type for working with anonymous structures that are intended to hold collections of objects by the string key. It is designed to work with map.get(key) and map.set(key). It is basically a nicer wrapper around Reflect.field and Reflect.setField and does some safety checks with Reflect.hasField.
var variable = "my_key";
var value = 123;
var dynamicMap = new haxe.DynamicAccess<Dynamic>();
dynamicMap.set(variable, value);
I'm noticing you are doing very much cast and dynamic, so untyped code, which is a bit of contradiction in a typed language. What is the actual type of dialogMap?
Not sure you are aware of it but, Haxe has its own maps, which are fully typed, so you don't need casts.
var map = new Map<String, Int>();
map[variable] = value;
I think this article helps understanding how to work with dynamic (untyped) objects.
Tip; for testing such small functionalities you can doodle around on the try.haxe site : http://try.haxe.org/#4B84E
Hope this helps, otherwise here is some relevant documentation:
http://api.haxe.org/haxe/DynamicAccess.html
https://haxe.org/manual/std-reflection.html
https://haxe.org/manual/types-dynamic.html
http://code.haxe.org/category/beginner/string-variable-reflection.html

Vala: Pass String as Class

Scenario:
I have x number of classes. Lets say 10; Each class does different UI Functions. When a user loads a file, that extension tells the program the classname to load; but it's in the form of a string.
Is there anyway to pass a string off as a classname? Something to the effect of.
var classname = "Booger";
var nose = new classname(){ //classname really means "Booger"
//Do Operation
}
You can reflect a type by name using var t = Type.from_name(classname);, however, this works on all types, including enums and structs and it might be the type Type.INVALID. You should probably do some checks, like t.is_a(typeof(MyParentClass)).
You can then instantiate a copy using var obj = Object.new(t);. The whole thing would look like:
var classname = "Booger";
var t = Type.from_name(classname);
if (t.is_a(typeof(MyParentClass)))
return Object.new(t);
else
return null;
It's also worth noting that the run-time type names have the namespace prepended, so you might want to do "MyNs" + classname. You can check in either the generated C or doing typeof(MyClass).name().
I've had the same problem as the OP in regards to getting an assertion error against null. If you take a look at the Glib documentation (in C) it mentions you have to register your class by actually specifying the class name first before you can actually use a string representation of your class name.
In other words you have to use your class first BEFORE you can instantiate a copy of your class with Glib.Type.from_name ("ClassName").
You can use your class first by instantiating a class instance or by getting type information for your class.
var type = typeof (MyClass);
var type_from_string = Type.from_name ("MyClass");
Furthermore, when you use Object.new to create a class there are two things you need to be aware of:
1) You need to cast the return value to get your specific class or base class.
var instance_of_my_class = Object.new (type) as MyClass;
2) Constructors for your class will no longer be called (I don't why). You will need to use the GObject style constructor inside your class:
construct {
pizza = 5;
}

How to create a new object using a class name in Java ME?

So far I couldn't find a method which doesn't require the Constructor class.
This class is not available in Java ME.
Is there any other way?
Note that the class's constructor takes parameters.
Is this what you are looking for?
String className = "java.lang.String";
Class theClass = Class.forName(className);
// this line will call the empty constructor
String s = (String) theClass.newInstance();
http://docs.oracle.com/javame/config/cldc/ref-impl/midp2.0/jsr118/java/lang/Class.html#newInstance%28%29

Error while adding data to ExpandoObject like dictionary

I am trying to understand c# dynamic. I have an ExpandoObject instance assigned to dynamic variable.
I understand ExpandoObject is implementing IDictionary. But the below assignment fails.
dynamic obj = new ExpandoObject();
obj["Test"] = "TestValue";
Console.WriteLine(obj.Test);
Can someone tell me where i am going wrong?
obj.Test="TestValue";
However the above statement seems to be working fine.
To do that, you need to cast the ExpandoObject to IDictionary<string, object>.
This is normal Expando usage:
obj.Test = "TestValue";
This is string-literal (or string variable) usage:
var d = (IDictionary<string, object>)obj;
d["Test"] = "TestValue";
string s = "Test";
d[s] = "TestValue";
If the interface implementation is explicit, you need to cast to a reference of the interface in order to access the members. I'm guessing this is what has happened here.

Resources