Deserializing Json string c# [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 months ago.
Improve this question
I am trying to deserialize the following json into a C# object? I need to read density and the coordinates. Can someone please check if the json is correct and the best way to get the data into C# object?
{{
"geometry": {
"type": "Point",
"coordinates": [
51.5570726284386,
25.39156280708102
]
},
"density": 1
}}
Thanks.

your json is not valid. But if you remove an extra braket at the start and the end of a string
json = json.Substring(1,json.Length-2);
then you can parse it
using Newtonsoft.Json;
var jsonObject = JObject.Parse(json);
int density = (int)jsonObject["density"];
double[] coordinates = jsonObject["geometry"]["coordinates"].ToObject<double[]>();

Use Parse(...):
dynamic d = JObject.Parse(json);
var density = d["density"];

Related

How to add variable in the object in C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
var data = #"{
""owner1"":""Name"",
}";
Need to add variable in the above object in c#
If your data is json string, you can modify it by JObject like this:
var jsonData = JObject.Parse(data);
jsonData["owner1"] = "owner1";
jsonData["owner2"] = "owner2";

Parsing JSON array with NewtonSoft JSON [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
My JSON looks like this:
[[["Text 1.A","Text 1.B",null,null,3],["Text 2.A","Text 2.B",null,null,1],["Text 3.A","Text 3.B",null,null,3],["Text 4.A","Text 4.B",null,null,3]],null,"en"]
and I need to get all the A texts into one string. There can be more than 4 values in the array.
I've tried searching online but for some reason I can't find a solution, or I don't understand the solution. I'm completely new to JSON so any help will be appreciated.
First, it's not correct JSON. I hope double quote is just a typo.
Second, your JSON looks extremely shapeless. It's an array of anything, first element is also an array of arrays with anything. That kind of structures have to be at some point parsed manually.
If I understand you correctly, you need those texts with .A. This should do the job:
string json =
"[[[\"Text 1.A\",\"Text 1.B\",null,null,3],[\"Text 2.A\",\"Text 2.B\",null,null,1],[\"Text 3.A\",\"Text 3.B\",null,null,3],[\"Text 4.A\",\"Text 4.B\",null,null,3]],null,\"en\"]";
var tokens = JsonConvert.DeserializeObject<JToken[]>(json);
var subArray = tokens[0].ToObject<JToken[]>();
var aTexts = subArray.Select(a =>
{
var arr = a.ToObject<object[]>();
return (string)arr[0];
}).ToArray();

Getting data part from JSON Array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have following output from JSON call in c#
{"Field": [
"PID",
"PName"],
"Data": [
[
5,
"A3"]
]
}
I want to get only Data part which is an array of data and to store in Data Table.That I have to do in c# so that I can manipulate it.
How can I achieve this ?
You can use Newtonsoft Library,
and use following line of code
Newtonsoft.Json.Linq.JObject obj = youJsonObject;
Newtonsoft.Json.Linq.JToken data= obj.GetValue("Data");
You can download and install nuget package for Newtonsoft from here
you can use JavaScriptSerializer this will convert json to C# object/List of objects
using System.Web.Script.Serialization
///
var serializer = new JavaScriptSerializer();
var object = serializer.DeserializeObject(jsonString) as (your object type)
"your object type" is a class you create to represent the data converted from json this will help you to manipulate it easily in C#

Remove Double quote from json Data [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Hello friends I have Some issue with jquery json. Below is my Json:
var dataSet2=[{"label":"Label 1","data":"[[1,10],[2,20],[3,10],[4,25],[5,15],[6,20],[7,40]]","color":"#3498db"},{"label":"Label 2","data":"[[1,15],[2,30],[3,25],[4,55],[5,30],[6,45],[7,50]]","color":"#e74c3c"}]
I want my Json Like:
var dataSet2=[{"label":"Label 1","data":[[1,10],[2,20],[3,10],[4,25],[5,15],[6,20],[7,40]],"color":"#3498db"},{"label":"Label 2","data":[[1,15],[2,30],[3,25],[4,55],[5,30],[6,45],[7,50]],"color":"#e74c3c"}]
need to remove double quotes
"[[1,10],[2,20],[3,10],[4,25],[5,15],[6,20],[7,40]]"
"[[1,15],[2,30],[3,25],[4,55],[5,30],[6,45],[7,50]]"
I have done with replace string option but its not working with my var dataSet2.
. Your help will be appreciable. Thanks in advance
What you need to do is not "remove double quotes". You need to either (a) parse the JSON arrays that are in the form of strings, or (b) produce the correct JSON in the first place.
You haven't shown us any of your C# code so I can't help you with (b), but for (a), you can do this:
var dataSet2 = [
{"label": "Label 1", "data": "[[1,10],[2,20],[3,10],[4,25],[5,15],[6,20],[7,40]]", "color": "#3498db"},
{"label": "Label 2", "data": "[[1,15],[2,30],[3,25],[4,55],[5,30],[6,45],[7,50]]", "color": "#e74c3c"}
];
// parse and replace .data properties
dataSet2.forEach(function(item) {
item.data = JSON.parse(item.data);
});
console.log(dataSet2);
your json string won't work anyway... (you have to have double quotes at the beginning and end)
try this:
string json = "[{\"label\":\"Label 1\",\"data\":[[1,10],[2,20],[3,10],[4,25],[5,15],[6,20],[7,40]],\"color\":\"#3498db\"},{\"label\":\"Label 2\",\"data\":[[1,15],[2,30],[3,25],[4,55],[5,30],[6,45],[7,50]],\"color\":\"#e74c3c\"}]";
Use JSON formatter to get an escaped (and usable) string out of it.

Simple JSON read in C# (was using Java - need it in .NET) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
In Java my json code is:
String result = ....some json string
JSONObject jObject = new JSONObject(result);
bearerToken = jObject.getString("access_token");
That's it!
I am trying to use newtonsoft in a C# program to do the same thing without setting up an object to deserialize to.
Thanks
JObject jObject = JObject.Parse(result);
string bearerToken = jObject.Value<string>("access_token");
Matt Johnson's answer is the most specific 1-1 translation.
However, if your Json contains more than one property, in .net you have dynamic which is less typing than .Value<string>("foo"); if you have to access several values.
This will fill the dynamic variable with your json string's properties:
var json = "{ access_token : \"SomeValue\" }";
dynamic jsonDto = JsonConvert.DeserializeAnonymousType(json, new ExpandoObject());
Console.WriteLine(jsonDto.someProp);

Categories