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"];
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
I want to parse this code into an object in C#. I was trying to find an answer but every post included data with key values which this doesn't have. Every piece of data is separated by semicolons.
[
[
1499040000000,
"0.01634790",
"0.80000000",
"0.01575800",
"0.01577100",
"148976.11427815",
1499644799999,
"2434.19055334",
308,
"1756.87402397",
"28.46694368",
"17928899.62484339"
]
]
To quick and easy get the object that Visual Studio think of the Json, you can copy the complete Json and then go to Edit > Paste Special > Paste JSON As Classes. For me, that generates the following for the complete Json that you posted:
public class JsonClass
{
public object[][] Property1 { get; set; }
}
(This could potentionally be object[] depending on incoming Json.)
This should be possible to JsonConvert into a List<JsonClass> and after that, depending on your scenario, try to parse the data to correct data type if that is needed.
I think that another way of doing it is also as #dbc mentioned in the comment:
You might be able to deserialize this to a List<T> for some appropriate T, where the members of T correspond to the array entries and you are using ObjectToArrayConverter<T> from here. Or you could just load using JArray.Parse(jsonString) from json.net.
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();
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.
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);