Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parse Json text to C# object in asp mvc 4

I have a huge amount of customized attributes I want to save them in the DataBase, I was confused of how to store them in the database, i thought of storing them as a string separating them by

(= => name , value) (; => attribute , attribute) but the code wasn't elegant at all!

so i stat thinking of saving them as Json string but I couldn't found a Json to object parser

while we need only to call json() to parse object to json string

is there a better way than using json string and is there json string parser provided ?

like image 899
Hilmi Avatar asked Nov 21 '12 08:11

Hilmi


People also ask

Can C use JSON?

JSON-C implements a reference counting object model that allows you to easily construct JSON objects in C, output them as JSON formatted strings and parse JSON formatted strings back into the C representation of JSON objects. It aims to conform to RFC 7159.

What is JSMN?

jsmn (pronounced like 'jasmine') is a minimalistic JSON parser in C. It can be easily integrated into resource-limited or embedded projects. You can find more information about JSON format at json.org. Library sources are available at https://github.com/zserge/jsmn.

Can we convert string to JSON in C?

Convert String to JSON Object With the JObject. Parse() Function in C# The JObject class inside the Newtonsoft. Json package is used to represent a JSON object in C#.


2 Answers

Try to use System.Web.Script.Serialization.JavaScriptSerializer, here is example:

var yourObject = new JavaScriptSerializer().Deserialize<YourType>(strInput)

or

var yourObject = new JavaScriptSerializer().Deserialize(strInput)
like image 163
testCoder Avatar answered Oct 16 '22 11:10

testCoder


Many people use Json.net for serialization

var log  = JsonConvert.DeserializeObject<YourObject>(logJson)

and the other direction

  var logJson = JsonConvert.SerializeObject(log);
like image 18
dove Avatar answered Oct 16 '22 09:10

dove