Extracting JSON from JSONP - c#

I am parsing a JSON file using C#. Here is what I got from the server:
loadData([
{"id":"id1","nm":"name1"},
{"id":"id2","nm":"name2"},
{"id":"id3","nm":"name3"}
]);
This is not the entire string, as I have deleted some of the values to make it appear more straightforward.
As you can see, this JSON is not parseable because it ends with semicolon (;) and has a bunch of other issues which need to be fixed.
Now that I have this data, is there any workaround I can do on the client side to parse this JSON?

The server gave you more than the JSON file. It gave you loadData(jsonData); with jsonData looking like this :
[
{"id":"id1","nm":"name1"},
{"id":"id2","nm":"name2"},
{"id":"id3","nm":"name3"}
]
So you will have to parse this to extract the JSON file.

Related

Corrupted JSON HTTP response

I am getting a HTTP request for a website and the content type is JSON. However, I am getting a nested JSON that is a unicode and is causing consistency problems.
Here is an example:
{"key1":"value",
"key2":"value",
"key3":{
u'key31':u'value',
u'key32':u'value'}}
This reminds me of python 2.7 troubles but I am not sure how to fix this JSON. I am using C# to parse it. Everything works correctly until I try to access key3.
The content should be a JSON object type but it is considered rather a value or a string.
Thanks for ya help. Is there a way to fix it if it is actually corrupted or am I parsing it wrongly?
You're correct that this json object is not complete / does not have the correct syntax. You're missing a closing '}' character.
How are you parsing your data? Try taking a look at this documentation.
your json object is not in valid formatted it should be like as folllows
{
"key1":"value",
"key2":"value",
"key3":{
" u'key31'":"u'value'",
"u'key32'":"u'value'"
}
}
by any chance do you get this json from python dump? coz Python's unicode literals are not valid JSON, and neither are single quotes

Convert Json that has many unicode characters back to standard json

I'm receiving a json payload from a webhook but the json that is returned is coming in like this
payload=%7B%22event%22%3A%7B%22info%22%3A%22REFRESH.INTERIM_PROGRESS%22%2C%22loginName%22%3A%22sbMemKostaSavR4%22%2C%22data%22%3A%7B%22providerAccount%22%3A%7B%22id%22%3A10376130%2C%22providerId%22%3A12292%2C%22isManual%22%3Afalse%2C%22createdDate%22%3A%222017-08-20%22%2C%22aggregationSource%22%3A%22USER%22%2C%22refreshInfo%22%3A%7B%22statusCode%22%3A0%2C%22statusMessage%22%3A%22OK%22%2C%22status%22%3A%22IN_PROGRESS%22%2C%22additionalStatus%22%3A%22ACCOUNT_SUMMARY_RETRIEVED%22%7D%7D%7D%7D%7D
is there a simple way to convert it back to normal standard json with the curly braces. I really don't want to do a crazy string.Replace method on it.
This worked for me.
HttpUtility.UrlDecode(json);
Easily with build-in java function:
java.net.URLDecoder.decode("YOUR_ENCODED_STR", "UTF-8");
No need any external library.3

Dynamic Json deserialization

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:\\\"} ...

Restsharp parsing names not supported by C# Json

Hi I'm making a web api client that returns stuff in json.
I'm using Restsharp that uses newtonsoft.json to deserialize json objects.
The problem is that the server returns an object with a property with #Text as name. Is there a way to make restsharp parse this?
Here is a sample:
image: [
{
#text: http://userserve-ak.last.fm/serve/34s/55125087.png
size: small
}]
All the other properties are being parsed just right the only problem is this one, the property is a string type so no problem in here.
Regards
That JSON isn't valid; you need to put quotes around the #text key (so make it "#text") as well as both values (so "http://...png" and "small").

converting json string into json on client side?

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.

Categories