How can I get and copy to table specified strings from response(I Think that it is same as getting from any text)?
I mean that for example I am getting response like:
PSEUDO CODE:
"blablabla... rank:1, name:string1, blablabla, rank:2, name:string2... "
I would like to get and copy string1,string2,string3,..., to table. How can I do IT?
You probably need (I'm not sure, question is not very clever) to parse JSON into a C# collection of some type (if the response is JSON) and then you can access data easily.
To parse JSON, see this question: How can I parse JSON with C#?
You can use Regex to match the names and get the values in an array.
string input = "blablabla... rank:1, name:string1, blablabla, rank:2, name:string2";
string[] result = Regex.Matches(input, #"name:(?<Name>[^,]+)")
.OfType<Match>()
.Select(o => o.Groups["Name"].Value)
.ToArray();
Assuming your result is really not JSON (see my comment to #Fire-Dragon-DoL), I would recommen parsing this with a regex:
use "name:([^,])" to capture a single string
Use either the combination IList-ToArray()-Join(",") or a StringBuilder to concatenate
Related
I have a literal string that contains details of a json array that i need to extract a value from in C#
The string looks like the following:
"{\"Field1\":[],\"Field2\":333,\"Field3\":\"string\"....
Now Field2 is the field i wish to get in this isntance, but i have no idea how to in C#
Check out the Newtonsoft.Json package on nuget.org, it can parse the JSON for you and then you can retrieve the keys by name
Since the value is in JSON format, use JSON.Net to deserialize it to form a C# type then you can read the value as you read any other property in a class
Another way (apart from using external packages/addons whatever) would be writing a small regex-function like this:
public string GetField(string fieldName)
{
Regex rgxGetField = new Regex(fieldName + "\\\":(.*?),");
Match mGetField = rgxGetField(jString);
return = mGetField.Groups[1].Value;
}
For sure only works for the format you posted in your question.
You want to deserialize the JSON string. See How to Deserialize JSON data? for a number of excellent answers.
thanks for all the help
i ended up using the following:
dynamic d = JObject.Parse(string);
field2= d.field2;
Deserialize JSON into C# dynamic object?
Following above question, I copy the dynamicJsonDeserilization and trying to use that in my application.
then I try to access the object as
var Data = json.deserilization(jsonstring);
Now, my string is
{"0":{"Name":"C:\\","Type":"Partition","Path":"C:\\"},"1":{"Name":"D:\\","Type":"Partition","Path":"D:\\"},"2":{"Name":"E:\\","Type":"Partition","Path":"E:\\"}}
i.e. I just have an Array on my server which I convert to JSON string and send.
As per code from best answer I should be able to access it as Data.0 but it give "End of Expression expected", Also Data[0] is giving same error. I am not sure how can I use it ? Any help is appreciated. Thanks.
Now, my string is
{"0":{"Name":"C:\","Type":"Partition","Path":"C:\"},"1":{"Name":"D:\","Type":"Partition","Path":"D:\"},"2":{"Name":"E:\","Type":"Partition","Path":"E:\"}}
Your string is indeed not valid JSON due to escaped quotes.
Those C:\ are breaking the parser. You should generate it like this, sending three backslahes:
{"0":{"Name":"C:\\\","Type":"Partition","Path":"C:\\\"} ...
In my javascript code I am getting json string from cs file
var tmpString="<%=resultset2%>";
In cs I am concatenating strings to build json string. Here is an issue the json string is returned as a string and it has " with it.
"[{id:'1',name:'Aik'},{id:'2',name:'Aik or Aik'}]"
Because of " in beginning and end javascript code treat it as a string. Kindly guide me how I should sort out this issue.
thanks
Fix the JSON, it has errors (property names must be strings (and thus quoted), and only " are acceptable for quoting strings in JSON). JSON is a subset of JavaScript, you can't use all of JS' syntax in JSON. As a rule of thumb, if you are concatenating strings to produce a data format, then you are doing it wrong. http://json.org/ lists a number of C# libraries that you can use to build JSON.
Use json2.js
Change this:
var tmpString="<%=resultset2%>";
to:
var tmpString=<%=resultset2%>;
This isn't JSON, you're just writing javascript from a server page. The problem is you are creating invalid javascript syntax, you just need to remove the quotes.
The quotes aren't from resultset2 they are from your markup.
I am writing an application to get the json object from server.
for example:
{"23423423", [abc, 2009-10-12, hello]}
My problem is:
if abc is a string that contains comma, then how can I parse the content in square brackets?
normally it should be three items in the square brackets. But if abc contains a comma, then I will get four items, which is not right.
Any ideas ?
Thanks in advance !
EDIT:
JSONObject obj = new JSONObject();
List list = new ArrayList();
list.add("abc");
list.add("2009-10");
obj.put("234234", list.toString());// don't use toString();
Finally I solve it, I should not use the list.toString(), otherwise the whole list will be converted to a string.
If abc is a string, then it should be coming from the server quoted, as "abc". If it isn't, then whatever created the JSON is doing it wrong.
A decent JSON parser handles that. Why not just use one of the existing C# JSON parsers out there, such as JSONSharp?
If I have the following plain string, how do I divide it into an array of three elements?
{["a","English"],["b","US"],["c","Chinese"]}
["a","English"],["b","US"],["c","Chinese"]
This problem is related to JSON string parsing, so I wonder if there is any API to facilitate the conversion.
use DataContract serialization http://msdn.microsoft.com/en-us/library/bb412179.aspx
I wrote a little console example using regex there is most likely a better way to do it.
static void Main(string[] args)
{
string str = "{[\"a\",\"English\"],[\"b\",\"US\"],[\"c\",\"Chinese\"]}";
foreach (System.Text.RegularExpressions.Match m in System.Text.RegularExpressions.Regex.Matches(str, #"((\[.*?\]))"))
{
Console.WriteLine(m.Captures[0]);
}
}
ASP.NET MVC comes with methods for easily converting collections to JSON format. There is also the JavaScriptSerializer Class in System.Web.Script.Serialization. Lastly there is also a good 3rd party library by James Newton called Json.NET that you can use.
Remove the curly braces then use String.Split, with ',' as the separator.
Unfortunately, I never did JSON stuff so don't know a parsing library. Can't you let WCF do this stuff for you ?
using String.Split won't work on a single token as the string in each token also contain a string (if I understood the requirements, the array elements should end up being:
["a","English"]
["b","US"]
["c","Chinese"]
If you use string.Split and use a comma as the delimiter the array will be made up of:
["a"
"English"]
["b"
"US"]
["c"
"Chinese"]
A JSON parser that I've read about but never used is available here:
http://james.newtonking.com/pages/json-net.aspx