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.
Related
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 2 years ago.
Improve this question
I'm trying to help someone out and this isn't my area of expertise so thought maybe someone can help me help someone else.
I have a field called Master that contains an array. I also have a field called Original that contains a string. I want to check if the string in Original is in the array field called Field1 and then with an if statement do something if true / false
"Original":"1234",
"Master":[{"ID":1,"Field1":12345},
{"ID":2,"Field1":123456},
{"ID":3,"Field1":1234},
{"ID":4,"Field1":12344}]
The array can be different each time and have a different amount of records in it.
Can anyone help me?
if Original and Master would be properties of the same class and in a variable named instance then you could use Linq to do:
bool isPresent = instance.Master.Any(entry => entry.Field1 == instance.Original);
Obviously you would need to first serialize the json to an instance of this class.
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 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#
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 7 years ago.
Improve this question
There is my data.
How to convert that to Model?
{"test": ["123","456"]}
If you already have a json string and want it to map it to a C# class construct you can use the intigrated Visual Studio function Paste Json as Classes.
Copy some JSON
Select Edit –> Paste Special –> Paste JSON As Classes
If you do so Visual Studio will make for you this class:
public class Rootobject
{
public string[] test { get; set; }
}
Side note:
If you are not using Visual Studio you can go visit this site. Which will provide you the same feature with a similar result.
To deserialize you simply call:
var json = "{\"test\": [\"123\",\"456\"]}";
var myObject = JsonConvert.DeserializeObject<Rootobject>(json);
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.