I wanna send JSON response to browser that requested based on REST service. I use something like this that includes some quotes in Controller Method:
return Json("blah\"blah", JsonRequestBehavior.AllowGet);
And I expect the result would be blah"blah But is blah\"blah and includes back slash too! I wanna have blah"blah in response without any conversion in client side. I know that need to perform this via C# codes but how to do that?
C# and JSON encode characters similarly. The string blah"blah is encoded in both C# and JSON as "blah\"blah". It's perfectly expected, then, that your raw JSON includes the backslash.
When you decode that string with a proper JSON library, it again becomes the string blah"blah.
I found the answer in two above threads:
ASP.Net MVC: how to create a JsonResult based on raw Json Data
How to output Json string as JsonResult in MVC4?
So that I need to use something like this:
return Content(jsonStringFromSomewhere, "application/json");
With this in mind considering that JSONP is used in case of ajax requesting to external service or URL. But I wanna build specific string due to parse with a special parser and I used JSON rather than JSONP and result was great.
Related
so I have a json string within my model data that is sent to the view for display in a table.
I am wanting to be sure it is displayed in a formatted fashion instead of one line string.
My research has led me to find this to be the cleanest method...
string json = JsonConvert.SerializeObject(account, Formatting.Indented);
however, within the view, once my value is extracted to #item.requestExample (the json string to be formatted), can I can call this c# to return the formatted string to the html?
btw, I've tried a few other methods just js, but every time the #item.requestExample is used within the , the inspect/console complains of the invalid tokens of the string since the string it an html representation that is using "e; instead of "'s.
tia
Maybe you could parse the json (if it is json, I'm not sure I understood well) string into a dynamic object. Then you can iterate over the properties and visualize them at your will.
You can see how to do that here.
Deserialize JSON into C# dynamic object
I have a Web Api project and a controller receives a Json parameter like this
public HttpResponseMessage QueryRead([System.Web.Http.ModelBinding.ModelBinder(typeof(WebApiDataSourceRequestModelBinder))] DataSourceRequest request, string queryparams, int qryId)
The string queryparams is actually a Json object that the client sends in the form
"{\"Supplier\":{\"name\":\"Supplier\",\"type\":\"PK\",\"textvalue\":\"[{\"Supplier_Key\":2,\"Supplier_Value\":\"Foxes Beverages \"}]\",\"value\":\"[2]\"}}"
This is a valid json object.
The problem is that the string queryparams removes all backslashes and the string becomes
{"Supplier":{"name":"Supplier","type":"PK","textvalue":"[{"Supplier_Key":2,"Supplier_Value":"Foxes Beverages "}]","value":"[2]"}}
It ommits all backslashes and the later is not a valid json
Any ideas?
If your client is really sending you
"{\"Supplier\":{\"name\":\"Supplier\",\"type\":\"PK\",\"textvalue\":\"[{\"Supplier_Key\":2,\"Supplier_Value\":\"Foxes Beverages \"}]\",\"value\":\"[2]\"}}"
...the trouble is at the client end. It looks like double-encoded JSON, as though they'd done this (in JavaScript):
var json = JSON.stringify(originalObject);
var json2 = JSON.stringify(json);
...but the textvalue part is wrong even for double-encoded JSON, so it's not that simple. So if (again) they're really sending that to you, the problem is at their end. Moreover, you can't correct it. If it were "just" double-encoded JSON, you could correct for it by parsing it twice. But it isn't, because of the value of textvalue, so it's nonsense (if it's really like that).
I am working on a scraper and I got stuck trying to figure out a base64 encrypted data being passed through AJAX that contains a products Size and Color information.
Sample product URL:
http://merchant.com/MARC-by-Marc-Jacobs-Orion-Metallic-Taffeta-1950s-Dress-Black/prod174690614/p.prod?eVar4=You%20May%20Also%20Like&RST=CategorySiloedViewCP
Using Fiddler, I can see that it calls an AJAX service to retrieve the sizes and colors for this product: http://merchant.com/productserviceAJAX
It passes the following string:
data=$b64$eyJQcm9kdWN0U2l6ZUFuZENvbG9yIjp7InByb2R1Y3RJZHMiOiJwcm9kMTc0NjkwNjE0In19×tamp=1424678606863
I need to be able to determine how to recreate the above string to be able to pass it to the service and I can get the returned JSON string from the AJAX call.
That's not an encryption, that's an encoding. Decode the base64 string, e.g. using an online tool and look at the result:
{"ProductSizeAndColor":{"productIds":"prod174690614"}}
This is a JSON expression, the product ID appears in your Sample product URL, so you should be able to construct a Base64 string from a given URL using C# Base64 methods.
I am trying to scrape Google search suggestion using c# but am unable to parse the response which looks like json.
The url I am using is
http://clients1.google.com/complete/search?client=youtube&hl=en&gl=us&gs_rn=23&gs_ri=youtube&ds=yt&cp=2&gs_id=d&q=jk
and here is an example of response data
window.google.ac.h(["jk",[["jk news",0],["jkfilms",0],["jk party",0],["jkt48 kokoro no placard",0],["jkt48 river",0],["jk simmons",0,[3]],["jkn",0],["jkt48",0],["jk rowling",0],["jkt48 fortune cookie",0]],{"q":"M9pm0qoSNfax1agFT10pPSqRq54","j":"d","k":1}])
I have tried using json.net and string operations like trim,replace,remove etc without any success
Is there any easy way to get the suggested keywords into an array?
Assuming that it always starts with window.google.ac.h( and ends with ) then you can just do:
var json = input.Replace("window.google.ac.h(", "").TrimEnd(')');
Which produces valid JSON according to http://jsonlint.com/, which you can always put into JSON.NET or similar.
P.S. Scraping this kind of thing may be against Google's ToS, I suggest you read them.
I want to embed an xml string in a json string. I am returning this json from a web method and at client side I have to extract the xml string from this json data.
I tried this:
var data= $.parseJSON(jsonResponse);
But as the jsonResponse contains XML data it is becoming an invalid json and becomes unable to parse.
Is there any way to successfully embed xml string in json and extract it ?
EDIT:
Tried encoding xml string :
System.Security.SecurityElement.Escape(xmlString)
and then added it to json string.
Still at client side the json couldn't be parsed
EDIT
tried Ted Johnson's solution and the problem is partially fixed.
Now I could parse the json and extract the other attributes. But on accessing the xml attribute, it says undefined. Also couldn't decode it.
You will need to do the following.
Ensure the XML is encoded to project quote escaping. As the XML will need to be parsed as a string. In c# there is a standard way, URL Encoding using C#
ParseJSON
Access JSON attribute which has the xml encoded as a string and decode it. http://www.w3schools.com/jsref/jsref_decodeuri.asp
Parse the XML ... http://api.jquery.com/jQuery.parseXML/ and save result for use.