I'll parse http://www.finam.ru/scripts/export.js
Since that javascript data looks like comma seperated values, just remove javascript stuff like
leading var aEmitentIds=new Array( and
trailing );
and use csv library to parse the data.
Related
I have MemoryStream data (HTML POST Data) which i need to parse it.
Converting it to string give result like below
key1=value+1&key2=val++2
Now the problem is that all this + are space in html. Am not sure why space is converting to +
This is how i am converting MemoryStream to string
Encoding.UTF8.GetString(request.PostData.ToArray())
If you are using Content-Type of application/x-www-form-urlencoded, your data needs to be url encoded.
Use System.Web.HttpUtility.UrlEncode():
using System.Web;
var data = HttpUtility.UrlEncode(request.PostData);
See more in MSDN.
You can also use JSON format for POST.
I suppose that the data you are retrieving are encoded with URL rules.
You can discover why data are encoded to this format reading this simple article from W3c school.
To encode/decode your post string you may use this couple of methods:
System.Web.HttpUtility.UrlEncode(yourString); // Encode
System.Web.HttpUtility.UrlDecode(yourString); // Decode
You can find more informations about URL manipulation functions here.
Note: If you need to encode/decode an array of string you need to enumerate your collection with a for or foreach statement. Remember that with this kind of cycles you cannot directly change the cycle variable value during the enumeration (so probably you need a temporary storage variable).
At least, to efficiently parse strings, I suggest you to use the System.Text.RegularExpression.Regex class and learn the regex "language".
You can find some example on how to use Regex here; Regex101 site has also a C# code generator that shows you how to translate your regex into code.
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
string result="12334,23432,3453455";
I am getting this string through Ajax call but it gives me the following error:
"unexpected non-whitespace character after JSON data"
When I remove comma's between strings it works fine .How to handle this?. I want to put value in textarea with comma's after the Ajax call
Whatever's outputting that isn't doing so in JSON format, but more like CSV.
A few options:
If you're able, fix the output method to correctly output JSON
Parse the string like a CSV
e.g. "12334,23432,3453455".split(',')
Conform the output to JSON first, then parse
e.g. JSON.parse("["+"12334,23432,3453455"+"]") (wrap with [])
Specify dataType:'text' in your $.ajax call.
Options 1-3 of the above would result in [12334,23432,3453455] as a javascript array of numbers, while Option 4 will simply result in "12334,23432,3453455" as a string.
BTW, using JSON.NET, this is what it should result in:
// As an array:
Int32[] ary = new[]{ 12334, 23432, 3453455 };
Console.WriteLine(JsonConvert.SerializeObject(ary));
// [12334,23432,3453455]
// As a string:
String str = "12334,23432,3453455";
Console.WriteLine(JsonConvert.SerializeObject(str));
// "12334,23432,3453455"
Your data has to be parsed by your JSON parser.
If your data is an array, your string should look like:
"[12334,23432,3453455]"
or should it be astring:
"\"12334,23432,3453455\""
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 have a c# program that retrieve some JSON data and use Newtonsoft JSON to Deserialize it.
as i use persian chars in my program the JSON codes will be shown like this:\u060c \u067e\u0644\u0627\u06a9 .... also after i retrive the JSON data in my program this chars still show like its coded sample.but after i Deserialize it converted to ???? chars.
what should i do?
Your JSON deserializer is broken; \uXXXX is supposed to be turned into proper characters.
To do that yourself, use this function
// Turns every occurrence of \uXXXX into a proper character
void UnencodeJSONUnicode(string str) {
return Regex.Replace(str,
#"\\u(?<value>[0-9a-f]{4})",
match => {
string digits = match.Groups["value"].Value;
int number = int.Parse(digits, NumberStyles.HexNumber);
return char.ConvertFromUtf32(number);
});
}
(Untested code; I don't have VS available at the moment. Some exception handling would probably be nice too)
Looks like it has been JSON encoded, so you need to decode it. The DataContractJsonSerializer class can do this.
See this MSDN link for more information.