How to deserialise PHP Serialized array in C# - c#

I've got an array string serialised via PHP (Wordpress) similar to the following:
a:2:{i:0;s:12:"Sample array";i:1;a:2:{i:0;s:5:"Apple";i:1;s:6:"Orange";}}
How would I deserialise this string in C#?
If I paste the string into https://www.unserialize.com, it converts the structure fine.
Thanks!

You can use KaitaiStruct library.
Example(source):
// Parse a local file and get structure in memory
var data = PhpSerializedValue.FromFile("path/to/local/file.php_serialized_value");

Related

format dynamic json string from model for display in .net view

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 &quote; 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

Reading more json strings from one file

I've got two different json files and would like to merge them and read json strings from one file.
{"ipAddress":"1.1.1.1","port":80, "protocol":"http"}
{"ipAddress":"1.1.1.1", "domainName":"domain.com"}
I tried something, but it still doesn't work properly. I tried array and also the following structure:
{"jsonString1": {"ipAddress":"1.1.1.1","port":80, "protocol":"http"},
"jsonString2": {"ipAddress":"1.1.1.1", "domainName":"domain.com"}}
Not sure if the structure is correct. I just need to get "jsonString1", "jsonString2" separately so I don't need to use more json files.
Your 1st fragment is non standard (effectively, not JSON).
Your 2nd IS standard, but is an object, not an array.
If you want an array, use an array:
[{"ipAddress":"1.1.1.1","port":80, "protocol":"http"},
{"ipAddress":"1.1.1.1", "domainName":"domain.com"}]
Alternatively, if you want to use your 2nd version (which is an object), you can access the 2 "sub-objects" by keys: myObj.jsonString1, myObj.jsonString2. BTW, A better name would be "Obj1" & "Obj2" since these are not strings, they're actual objects.
Use JSON Array to keep those files merged.For example
Conside,You have an JSON array for URL then and you want to print it in a paragraph then
<p id="url"></p>
declare a variable like this
var URL=[{"ipAddress":"1.1.1.1","port":80, "protocol":"http"},
{"ipAddress":"1.1.1.1", "domainName":"domain.com"}]
you can access these array by using
document.getElementById("url").innerHTML =
URL[0].ipAddress+ " " + URL[0].port+ " " +URL[0].protocol;
You can get the array values by using the index values
Thank you. I have finally used this syntax:
json:
{"jsonString1": {"ipAddress":"1.1.1.1","port":80, "protocol":"http"},
"jsonString2": {"ipAddress":"1.1.1.1", "domainName":"domain.com"}}
read the string using this command:
var jsonObject = JObject.Parse(jsonData);
string value = jsonObject["jsonString1"].ToString();

Embedd xml in json

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.

How to deserialize javascript objects to XML (not JSON) in C#?

I am using the following code to convert JSON string to XML for processing in C#:
XmlDictionaryReader xdr = JsonReaderWriterFactory.CreateJsonReader(System.Text.Encoding.Unicode.GetBytes(jsonStr), new XmlDictionaryReaderQuotas()); // ### QUOTA MAX WILL FIX THE MISSING CLOSING TAGS?
XElement root = XElement.Load(xdr); // Here I get the exception: The token '"' was expected but found '
I have used it with no problem until I tried to parse JSON with strings not enclosed by double quotes (which by the way, I think is perfectly fine with JSON format)
For example:
{string:"value"}
Can any body tell me how can I serialize JSON in C# so that this kind of simple format particulary wont break the code or generate exceptions?
EDIT:
I think what I am reading is not a JSON object but a javascript object like the following:
What's the difference between Javascript Object and JSON object
So question would be: How to deserialize javascript objects to XML (not JSON) in C#?

How to convert json from one format to another

I have a control that expects JSON in a particular format. I have a service that spits out JSON in a different format. What's the best way to convert the JSON to the expected format? C#, jQuery? I think I'd prefer to do it server-side.
With C# you can use System.Web.Script.Serialization.JavaScriptSerializer to parse your JSON string to an object or IDictionary.
MSDN: JavaScriptSerializer Class (System.Web.Script.Serialization)
With that object you could do anything you like, change values or maybe just parse it back to something like XML.

Categories

Resources