Convert JSON String to JSON Object c#

2019-01-10 22:02发布

I have this String stored in my database:

str = "{ "context_name": { "lower_bound": "value", "upper_bound": "value", "values": [ "value1", "valueN" ] } }"

This string is already in the JSON format but I want to convert it into a JObject or JSON Object.

JObject json = new JObject();

I tried the json = (JObject)str; cast but it didn't work so how can I do it?

7条回答
对你真心纯属浪费
2楼-- · 2019-01-10 22:21

This works for me using JsonConvert

var result = JsonConvert.DeserializeObject<Class>(responseString);
查看更多
We Are One
3楼-- · 2019-01-10 22:22

if you don't want or need a typed object try:

using Newtonsoft.Json;
// ...   
dynamic json  = JsonConvert.DeserializeObject(str);

or try for a typed object try:

Foo json  = JsonConvert.DeserializeObject<Foo>(str)
查看更多
beautiful°
4楼-- · 2019-01-10 22:24

This works

    string str = "{ 'context_name': { 'lower_bound': 'value', 'pper_bound': 'value', 'values': [ 'value1', 'valueN' ] } }";
    JavaScriptSerializer j = new JavaScriptSerializer();
    object a = j.Deserialize(str, typeof(object));
查看更多
不美不萌又怎样
5楼-- · 2019-01-10 22:28

You can try like following:

string output = JsonConvert.SerializeObject(jsonStr);
查看更多
等我变得足够好
6楼-- · 2019-01-10 22:29

JObject defines method Parse for this:

JObject json = JObject.Parse(str);

You might want to refer to Json.NET documentation.

查看更多
萌系小妹纸
7楼-- · 2019-01-10 22:33

there's an interesting way to achive another goal which is to have a strongly type class base on json with a very powerfull tools that i used few days ago for first time to translate tradedoubler json result into classes

Is a simple tool: copy your json source paste and in few second you will have a strongly typed class json oriented . In this manner you will use these classes which is more powerful and simply to use.

I hope that can help you

查看更多
登录 后发表回答