How To Parse JSON Object Using Regular Expression in C# [duplicate] - c#

This question already has answers here:
Regex To Extract An Object From A JSON String
(3 answers)
Closed 8 years ago.
string sample = "{\"STACK_SIZE\":4,\"thes_stack\":[4,4]}";
how can I parse it using RE in C#?

First of all this isn't a valid JSON, remove the backslashes.
Second, using a library like JSON.NET you can parse your sample.
string sample = "{"STACK_SIZE":4, "thes_stack":[4,4]}";
var parsed = JsonConvert.DeserializeObject<dynamic>(sample);
that will parse it into a dynamic type, if you want something more strongly typed create your own class:
class StackInfo
{
public int STACK_SIZE {get; set;}
public int[] thes_stack {get; set;}
}
then you can deserialize into it:
string sample = "{"STACK_SIZE":4, "thes_stack":[4,4]}";
var parsed = JsonConvert.DeserializeObject<StackInfo>(sample);
But since you didn't put exactly what you need or exactly what your problem is with the suggestions in the comments no one can really help you.

Related

Find a set of characters in a string C# class file(.cs) [duplicate]

This question already has answers here:
How can I check if a string exists in another string
(10 answers)
Closed 5 years ago.
I'm working on a Existing Class file(.cs) which fetches a string with some data in it.
I need to check if the string contains a word. String has no blank spaces in it.
The string-
"<t>StartTxn</t><l>0</l><s>0</s><u>1</u><r>0</r><g>1</g><t>ReleaseUserAuthPending</t>"
I need to check if the string contains 'ReleaseUserAuthPending' in it.
You can try this:
var strValue = "<t>StartTxn</t><l>0</l><s>0</s><u>1</u><r>0</r><g>1</g><t>ReleaseUserAuthPending</t>";
if (strValue.Contains("ReleaseUserAuthPending"))
{
//Do stuff
}
Refer About String - Contains function
For your information: Contains function is case-sensitive. If you want to make this Contains function as case-insensitive. Do the following step from this link.
bool containsString = mystring.Contains("ReleaseUserAuthPending");
Try
String yourString = "<t>StartTxn</t><l>0</l><s>0</s><u>1</u><r>0</r><g>1</g><t>ReleaseUserAuthPending</t>";
if(yourString.Contains("ReleaseUserAuthPending")){
//contains ReleaseUserAuthPending
}else{
//does not contain ReleaseUserAuthPending
}

c# datatype for json time [duplicate]

This question already has an answer here:
Parsing ISO Duration with JSON.Net
(1 answer)
Closed 6 years ago.
I got the following json result from a rest api and I am using Newtonsoft.Json to deserialize it to a c# object.
{
"d": {
"results": [
{
"Aufnr": "4000103",
"Workdate": "/Date(1482796800000)/",
"Beguz": "PT07H30M00S",
}
]
}
}
Aufnr and Workdate are working with String and DateTime, but I got no clue which datatype to use for Beguz
I tried TimeSpan and DateTime and got this error: Error converting value "PT07H30M00S" to type 'System.TimeSpan'. Path '[0].Beguz'
Any ideas?
This is a pure string:
public string Beguz { get; set; }
Of course if you want this PT07H30M00S string to be represented by some complex custom structure you could write a custom JsonConverter to achieve this task. In this converter you will need to provide the logic of how to parse this string back to some custom structure of yours.

how to read the variables inside a JSON string [duplicate]

This question already has answers here:
Parse Json string in C#
(5 answers)
Closed 6 years ago.
i have json string as following
string json = "{\"ID\":\"hid\",\"Specification\":\"hname\"}";
but i want to read Id and hname as following
string hid = hardwareidTextbox.Text;
string hname = hardwarename_Textbox.Text;
how do i read variable vales in JSON string
Try using
JsonConvert.DeserializeObject(json);

Writing contents of a class to a file [duplicate]

This question already has answers here:
Best practices for serializing objects to a custom string format for use in an output file
(8 answers)
Closed 7 years ago.
I have a class, all string properties like this:
public class MyClass
{
public string Name {get;set;}
public string Phone {get;set;}
// a bunch of more fields....
{
And a list of that class List<MyClass> myListOfObjects; that I have populated it through out the program with values.
And then I have a file (csv) with headers:
Name, Phone, etc...
I want to write the contents of that myListOfObjects into that file.
I was thinking just loop through them and write one row per object. But wanted to see is there a better nicer way?
You can write all your data in one shot, like
var list = new List<MyClass>();
var fileData = list.Select(row => string.Join(",", row.Name, row.Phone, row.Etc)).ToArray();
File.WriteAllLines(#"C:\YourFile", fileData);
Note: This is one way to improve the file write, but it doesn't handle un-escaped text data like Name with comma.

Extracting a URL in the query part of another URL [duplicate]

This question already has answers here:
Get URL parameters from a string in .NET
(17 answers)
Closed 9 years ago.
How can I extract a valid URL from a string like this one
h*tps://www.google.com/url?q=h*tp://www.site.net/file.doc&sa=U&ei=_YeOUc&ved=0CB&usg=AFQjCN-5OX
I want to extract this part: h*tp://www.site.net/file.doc, this is my valid URL.
Add System.Web.dll assembly and use HttpUtility class with static methods.
Example:
using System;
using System.Web;
class MainClass
{
public static void Main (string[] args)
{
Uri uri = new Uri("https://www.google.com/url?q=http://www.site.net/file.doc&sa=U&ei=_YeOUc&ved=0CB&usg=AFQjCN-5OX");
Uri doc = new Uri (HttpUtility.ParseQueryString (uri.Query).Get ("q"));
Console.WriteLine (doc);
}
}
I don't know what your other strings can look like, but if your 'valid URL' is between the first = and the first &, you could use:
(?<==).*?(?=&)
It basically looks for the first = and matches anything before the next &.
Tested here.
You can use split function
string txt="https://www.google.com/url?q=http://www.site.net/file.doc&sa=U&ei=_YeOUc&ved=0CB&usg=AFQjCN-5OX";
txt.split("?q=")[1].split("&")[0];
in this particular case with the string you posted you can do this:
string input = "your URL";
string newString = input.Substring(36, 22) ;
But if the length of the initial part of the URL changes, and also the lenght of the part you like to extract changes, then would not work.

Categories

Resources