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 years ago.
Improve this question
I need to serialise only some fields from Offices, only Name and ID
public IEnumerable<Office> Offices;
public string SerializedPointOffices
{
get { return JsonConvert.SerializeObject(Offices ); }
}
I have one Linq select
IEnumerable<MyObj> offices = GetAllOffices();
var test = offices.Select(t => new { t.OfficeName, t.OfficeID});
How Serialize this test?
type of test is WhereSelectArrayIterator
I am not entirely sure of your question, however, assuming you want to serialize an array or list of anonymous types to JSON, its pretty simple.
as above:
var test = offices.Select(t => new { t.OfficeName, t.OfficeID});
string str = JsonConvert.SerializeObject(test);
You can also use the Data Contract serialize annotations on your office object so that only the fields you want get serialized when you serialize the object. Personally, I prefer that way because I don't like sending anonymous types over the wire, I like having a strongly defined interface.
Related
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 is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Assuming we have a function:
public IEnumerable<object> GetViewModel(int id) {
var records = LoadRecordById(id);
var result = (from record in records
select new {
field1 = record.data1,
field2 = record.data2,
....
}).ToArray();
return result;
}
We can return it like IEnumerable<object> and like IEnumerable<dynamic>. The question is: what to use and why? what advantages/disadvantages ?
You have an anonymous type right now.
If you cast it to object, you will have no way to get the fields out. You cannot cast it back to an anonymous object (since you don't know what it was) and you can't access the properties from object.
If you cast it to dynamic, you can access the fields as you normally would, but you will lose all type safety. It will determine the type at compile time, which means you have access to the types generated at compile time, but if you type something wrong you will get a compilation error at runtime.
Best thing to do is to create a class that has the properties you need and return a list of that class. Then you will get the properties, and will get the benefits of static typing.
You should use an interface or a base class. But to answer your question:
dynamic foo = "Some string";
foo.ToUpper(); // this works fine
foo.Something(); // compiles, but runtime error
object bar = "Some string";
bar.ToUpper(); // does not compile
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);
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 years ago.
Improve this question
I am using Newton's Json.Net to deserialize Json to a .NET object. I am trying to understand whether one can cast from a general object to a specific object.
ie:
object myObject JsonConvert.DeserializeObject<object>(myJsonValue);
myOrder = (Order)myObject;
The code is more about decribing my question. I understand that one could do this:
object myObject JsonConvert.DeserializeObject<order>(myJsonValue);
But due to the fact this is a helper function then it could not be "order", but something more general.
Thanks.
EDIT:
Getting the error:
Getting Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Order'
I'm going to make an assumption here because the question isn't 100% clear, but I think I've got a good idea:
But due to the fact this is a helper function then it could not be "order", but something more general.
You need to add a type argument to your helper function:
public class SomeHelper
{
public T Deserialize<T>(string json)
{
return JsonConvert.DeserializeObject<T>(json);
}
}
And to use it:
SomeHelper helper = new SomeHelper();
string json = "...";
Order order = helper.Deserialize<Order>(json);
Rather than deserialize to an object, it's easier to work with something deserialized to JToken.
JToken myObject = JsonConvert.DeserializeObject<JToken>(myJsonValue);
You can then use the JToken.ToObject<T>() method to perform the final conversion:
Order order = myObject.ToObject<Order>();