Parsing JSON array with NewtonSoft JSON [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 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();

Related

How to check array for a value 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 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.

How to parse a JSON list into an object [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
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.

String contains a lot of copies, how to get rid of them? [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 7 years ago.
Improve this question
I got something like this:
string s="Solid;Solid;Gass;Solid;Solid;Gass;Solid;Gass;Liquid;Liquid;"
and now I want to get rid of the copies in the string...so that in the end s should be like this:
s="Solid;Gass;Liquid;"
Try this:
var parts = s.Split(';');
var distinctParts = parts.Distinct();
var newString = string.Join(";", distinctParts);
Where:
Split will give you an array with all the words of your string, taking the specified character as the word separator (; in this case).
Distinct will give your a collection with the unique words of your array.
Finally, Join composes a new string with the unique words, using the specified string (;in this case) as the separator.
You can split the string, then find the distinct instances and join them back in one line:
string s = "Solid;Solid;Gass;Solid;Solid;Gass;Solid;Gass;Liquid;Liquid;";
s = string.Join(";", s.Split(';').Distinct());

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);

Find what number a string is in a List [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 years ago.
Improve this question
I want to be able to know what number a string is in one list so I can match it with a string in another list.
Something like:
myList[1] would be 1
myList2[5] would be 5
Sorry if I'm not clear, but this is really hard to explain.
The number you're referring to is called the index.
You can find the index of a string in your list by using the List<T>.IndexOf() method.
int indexOfFoo = myList.IndexOf("foo");
To find the index of a string in a list of strings:
var myList = new List<string> { "x", "y", "z" };
var indexOfY = myList.IndexOf("y");
You can use myList.IndexOf("string") to get the index of the item in your list.
http://msdn.microsoft.com/en-us/library/e4w08k17(v=vs.110).aspx
I'm not sure if that is what you're asking, but that is how I understood it.
try it:
yourList.IndexOf("urstring")

Categories