[Closed] JSON parsing with vanilla dotnet
Here’s another approach.
If your json strings are flat (i.e not nested data) then you can use the JavascriptSerializer to extrapolate simple json strings like your example. The nature of serialization sort of assumes you know the structure of the data you are trying to write out. But it is possible with flat, arbitrary json strings. I’ve added them into a custom dotnet assembly, as I needed to use enumerable types and LINQ. There’s a method to write xml files from a json string, as well as a simple parse method returning a max compatible array if needed.
tested in max 2016/2017
-- lets register a parser for the data, it will return an array of keyvalue pair objects
fn buildJsonThingy =
(
if classof (dotnet.GetType "Woop.JsonThingy")!=dotNetObject then
(
local source = "using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Linq;
using System.Xml.Linq;
namespace Woop
{
public class JsonThingy
{
public Array Parse (string jsonText)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
Dictionary<string,string> dict = jss.Deserialize<Dictionary<string,string>>(jsonText);
List<KeyValuePair<string, string>> list = dict.ToList();
return list.ToArray();
}
public void ParseToXml(string jsonText, string xmlOutputFile)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
Dictionary<string,string> dict = jss.Deserialize<Dictionary<string,string>>(jsonText);
XElement el = new XElement(\"RootyTooty\",
dict.Select(kv => new XElement(kv.Key, kv.Value)));
el.Save(xmlOutputFile);
}
}
}"
csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
compilerParams.ReferencedAssemblies.AddRange #("System.dll","System.Web.Extensions.dll", "System.Core.dll", "System.Xml.dll", "System.Xml.Linq.dll")
compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
assembly = compilerResults.CompiledAssembly
assembly.CreateInstance "Woop.JsonThingy"
)
else dotnetobject "Woop.JsonThingy"
)
jObj = buildJsonThingy()
showmethods jObj
jsonStr1 = "{\"bring\": \"the_noise\", \"yeah\": \"meh\", \"beyonce\":\"lemonade\" ,\"justin\": \"bieber\", \"is\": \"shite\", \"woop\": true}"
jsonStr2 = "{\"Asset_Location\":\"la\",\"Asset_Name\":\"main\",\"Author\":\"changsooeun\"}"
jsonValues = jObj.Parse jsonStr1
for j in jsonValues do format "key:% value:%
" j.key j.value
jsonValues = jObj.Parse jsonStr2
for j in jsonValues do format "key:% value:%
" j.key j.value
jsonValues = jObj.ParseToXml jsonStr1 (pathconfig.appendPath (getdir #temp) "xmlOuptut1.xml")
jsonValues = jObj.ParseToXml jsonStr2 (pathconfig.appendPath (getdir #temp) "xmlOuptut2.xml")
The ParseToXml method builds the following from your example string. Bear in mind you might need to validate the string before writing it, as XML is picky about spaces in tags.
<?xml version="1.0" encoding="utf-8"?>
<RootyTooty>
<Asset_Location>la</Asset_Location>
<Asset_Name>main</Asset_Name>
<Author>changsooeun</Author>
</RootyTooty>