Here is the list array:
{"things":{"1":"1_4","3":"3_13","4":"4_18","5":"5_25","6":"6_28","7":"7_32"}}
here is the code:
var t = things;
string aa = t[0].ToString();
How do I split the above array in , separator and _ .
from the above array i want only 1_4,3_13...this column and save in two different var.
when I am using
string[] ss = aa.Split(new char[] { ',', '|', }, System.StringSplitOptions.RemoveEmptyEntries);
the problem is that in first list {"things":{"1":"1_4".
comes
so how to remove {"things"??
You should not parse JSON yourself. Do not reinvent the wheel.
You should define a class
public class MyClass
{
[JsonProperty("things")]
public Dictionary<int, string> Things { get; set; }
}
Then deserialize your string into an instance of MyClass
var rawData = "{\"things\":{\"1\":\"1_4\",\"3\":\"3_13\",\"4\":\"4_18\",\"5\":\"5_25\",\"6\":\"6_28\",\"7\":\"7_32\"}}";
var data = JsonConvert.DeserializeObject<MyClass>(rawData);
Then you can use data
Console.WriteLine(data.Things[1]);
You should add Newtonsoft.Json Nuget package to your project and add using Newtonsoft.Json namespace to your code file.
Instead of string manipulation, you should use JSON library like Json.Net. Simply, because you are working with string in JSON format and it will be easier to work with that information if you deserialize it into proper object or JObject.
Example with JObject:
string json = #"{""things"":{""1"":""1_4"",""3"":""3_13"",""4"":""4_18"",""5"":""5_25"",""6"":""6_28"",""7"":""7_32""}}";
var jObj = Newtonsoft.Json.Linq.JObject.Parse(json);
var prop1 = jObj.GetValue("things").GetPropertyValue("1").ConvertTo<string>();
var prop4 = jObj.GetValue("things").GetPropertyValue("4").ConvertTo<string>();
Note: You can create classes which will represent your JSON structure, if you want to have strongly typed object structure.
You should first split by { then discard the first and after that you are left with:
"1":"1_4","3":"3_13","4":"4_18","5":"5_25","6":"6_28","7":"7_32"}}
Now you split with , and get
"1":"1_4"
"3":"3_13" and so on.
Then you split each with : and take the second one.
Only remember that the last one is "7_32"}} which you should remove the trailing }}
Like this:
var elems = aa.Split(new[] { '{' })[1].Split(new[] { ',' });
var res = elems.Select(obj => obj.Trim("}".ToCharArray()).Split(new[] { ':' }));
Hope this is what you want :)
Related
given an apparently invalid json
(which comes from google)
https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=de&dt=t&q=Science
of
[[["Wissenschaft","Science",,,2]],,"en"]
i want to get the values of Wissenschaft and Science
if figured out a very inelegant way to do it with Json.net via
string h = "[[[\"Wissenschaft\",\"Science\",,,2]],,\"en\"] ";
var obj = JsonConvert.DeserializeObject<List<dynamic>>(h);
JArray arr1 = obj[0];
var arr2 = arr1.First;
var x = arr2.First.Next;
string s = x.ToString();
is there some better, less verbose way ?
Here is a more concise version, maybe somebody has one which also retains
the other values from the top array
string h = "[[[\"Wissenschaft\",\"Science\",,,2]],,\"en\"]";
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Error = (serializer, err) =>
{
err.ErrorContext.Handled = true;
//ignore all errors
};
var obj = JsonConvert.DeserializeObject<List<List<List<dynamic>>>>(h,settings);
string strWissenschaft = obj[0][0][0];
string strScience = obj[0][0][1];
As you see i only care for the values in the most nested array, the other values are lost.
For first this is not valid JSON object but nevermind as you said Json.NET will parse it by adding null values into empty commas.
Because this is not valid object and it is just an array in some JSON format. There will probably no better way then parse it into dynamic List as you already did.
In case of { } at start and end and some keys:values format you can deserialize it into C# object according to class which you can define.
Say I have a string representing an array of objects in JSON form:
string s = "[{\"name\":\"Person1\"},{\"name\":\"Person2\"}]";
What I want is an array of strings, each string being the string representation of a JSON object - NOT the object itself. It should look something like this:
string[] s = new string[]
{
"{\"name\":\"Person1\"}",
"{\"name\":\"Person2\"}"
};
1) Almost every search I attempt pulls up millions of results on how to simply deserialize a JSON string using (eg) Json.NET. This is not what I want to do.
2) I have tried building a class representing the objects to temporarily loop through a deserialize/serialize mapping each to a string in an array, but the schema for the objects is variable (hence why I only need a string representation).
3) I have attempted a few regex to try and do this, but my JSON string can contain fields that contain JSON strings as their value (icky, but out of my control) and so nested character escaping etc drove me partially mad before I decided to beg for help here.
Surely this should be simple? Anybody got any pointers?
You'll need to deserialize it, and then serialize each object independently.
For example (using Newtonsoft.Json):
string json = "[{\"name\":\"Person1\"},{\"name\":\"Person2\"}]";
var objects = JsonConvert.DeserializeObject<List<object>>(json);
var result = objects.Select(obj => JsonConvert.SerializeObject(obj)).ToArray();
Yields (as a string[]):
{"name":"Person1"}
{"name":"Person2"}
If you try to avoid deserializing and serializing, you're almost certain to run into an edge case that will break your code.
string s = "[{\"name\":\"Person1\"},{\"name\":\"Person2\"}]";
var Json = JsonConvert.DeserializeObject<List<object>>(s);
string[] Jsonn = Json.Select(x => x.ToString()).ToArray();
[] Jsonn returns string array instead of object array with JObject formatted.
Hope this one help you.
Why don't you just use this
string s = "[{\"name\":\"Person1\"},{\"name\":\"Person2\"}]";
string[] t = s.Split(',');
I tried it. It simply gives you string array as you want it....
The object has 139 fields; the string initializer may have some or all of these fields. It is formatted like this: "FirstName":"Bart","LastName":"Simpson","Company":"Fat Tony's","Address":"55 Maple Drive" etc. I could just look for the fields like this:
if (initializerString.contains("FirstName:")
FirstName="get the next series of chars until the ", or end of string
and so forth. But is there a more compact way to do this?
Seeing as your format is incredibly similar to JSON (except for the lack of braces, actually), as people commented you'll fare better by using JSON.NET.
If you have complete control over this string, just transform it into a json and deserialize it:
JsonConvert.DeserializeObject<YourClass>(yourString);
It will automatically set the correct properties while deserializing your data.
In case you don't have control of this format, and you need to parse it anyway, just put up the braces and you're good:
JsonConvert.DeserializeObject<YourClass>("{" + yourString + "}");
And if you don't have a specific class for this, you can also replace YourClass for a Dictionary<string,object>
You'll find this library as Newtonsoft.Json, and I believe it's the most popular library for dealing with JSON data.
I've made a working example so you can see it in action (note that I kept your string format, but please try to use straight json):
using System;
using System.Linq;
using System.Collections.Generic;
using Newtonsoft.Json;
public class Program
{
public class Information
{
public string FirstName{get;set;}
public string LastName{get;set;}
public string Company{get;set;}
public string Address{get;set;}
}
public static void Main()
{
string myObject = "\"FirstName\":\"Bart\",\"LastName\":\"Simpson\",\"Company\":\"Fat Tony's\",\"Address\":\"55 Maple Drive\"";
var converted = JsonConvert.DeserializeObject<Dictionary<string, object>>("{"+myObject+"}");
var converted2 = JsonConvert.DeserializeObject<Information>("{"+myObject+"}");
Console.WriteLine(String.Join("\n", converted.Select(c=> c.Key + ": " + c.Value)));
Console.WriteLine(converted2.FirstName);
}
}
And here's a bonus fiddle:
https://dotnetfiddle.net/fudUYZ
Using regex you could do:
string firstName = Regex.Match(yourstring,#"(?<=""FirstName"":"").*?(?="")").Value;
However this really looks like a json string and there are easier ways to get your data.
You could create a Dictionary<string,object> dict in which there is the name of the property and the property.
You can then split the string,
//Remove the "
initializerString = initializerString.Replace('"', '');
//Split by ,
var tmp = initializerString.Split(",");
//Foreach pair key/value split by :
foreach( var x in tmp){
var tmp2=x.Split(":");
//Assign the value to the property in the Dictionary
dict[tmp2[0]]=tmp2[1];
}
Warning. Since I don't actually know what you're using this code for, this is more of a general idea than working code.
I have a string in my C# model populated with this string:
"[{\"ta_id\":97497,\"partner_id\":\"229547\",\"partner_url\":\"http://partner.com/deeplink/to/229547\"},{\"ta_id\":97832,\"partner_id\":\"id34234\",\"partner_url\":\"http://partner.com/deeplink/to/id34234\"}]"
Is there a way, using LINQ or RegEx, that I could parse out the partner_id's - so I ended up with a list object with:
229547
id34234
Thanks for your help, Mark
I have never used any JSON parser but if it comes to Regex you could try something like this:
private static void regexString()
{
string myString = "[{\"ta_id\":97497,\"partner_id\":\"229547\",\"partner_url\":\"http://partner.com/deeplink/to/229547\"},{\"ta_id\":97832,\"partner_id\":\"id34234\",\"partner_url\":\"http://partner.com/deeplink/to/id34234\"}]";
string[] stringList = Regex.Split(myString, "},{");
for (int i=0; i<stringList.Length ;i++)
{
stringList[i] = Regex.Split(Regex.Split(stringList[i], "partner_id\\\":\\\"")[1], "\\\",\\\"partner_url\\\"")[0];
}
}
Also there is a nice website to help you with creating your own regex patterns in the future, check it out:
gskinner.com
And a nice and short tutorial:
www.codeproject.com
Assuming your link having partner id always-
string Name = "[{\"ta_id\":97497,\"partner_id\":\"229547\",\"partner_url\":\"http://partner.com/deeplink/to/229547\"},{\"ta_id\":97832,\"partner_id\":\"id34234\",\"partner_url\":\"http://partner.com/deeplink/to/id34234\"}]";
string[] splittedString = Regex.Split(Name, "}");
List<string> allIds = new List<string>();
foreach (var i in splittedString)
{
var ids =Regex.Split(i, "/");
string id = ids[ids.Length - 1];
allIds.Add(id);
}
If that is the general format of the string then this regex should work
(?i)(?<=(partner_id).{5})\w+
Test here
This from your string will get
229547 and id34234
(?i) = Case insesitivity
(?<=(partner_id).{5}) = postive lookbehind for parter_id then any 5 characters which in this case will be \":\"
\w+ = Any alphanumeric characters one or more times
Hope this helped
Since this is JSON, you probably shouldn't bother trying to get a regex working. Instead, you can parse the JSON and then use LINQ.
using System.Web.Script.Serialization; // (in System.Web.Extensions.dll)
...
string s = "[{\"ta_id\":97497,\"partner_id\":\"229547\",\"partner_url\":\"http://partner.com/deeplink/to/229547\"},{\"ta_id\":97832,\"partner_id\":\"id34234\",\"partner_url\":\"http://partner.com/deeplink/to/id34234\"}]";
JavaScriptSerializer j = new JavaScriptSerializer();
object[] objects = (object[])j.DeserializeObject(s);
string[] ids = objects.Cast<Dictionary<string, object>>()
.Select(dict => (string)dict["partner_id"])
.ToArray();
It's a little messy to deserialize it to an object, because you don't have any type information. If you're not afraid of making a small class to deserialize into, you can do something like this:
class Foo
{
public string partner_id
{
get;
set;
}
}
...
JavaScriptSerializer j = new JavaScriptSerializer();
string[] ids = j.Deserialize<Foo[]>(s).Select(x => x.partner_id).ToArray();
Note that there are other options for deserializing JSON. I simply chose the most general-purpose one that's built in.
Is there a straightforward way of converting:
string str = "a=1,b=2,c=3";
into:
dynamic d = new { a = 1, b = 2, c = 3 };
I think I could probably write a function that splits the string and loops the results to create the dynamic object. I was just wondering if there was a more elegant way of doing this.
I think if you convert the "=" into ":" and wrap everything with curly brackets you'll get a valid JSON string.
You can then use JSON.NET to deserialize it into a dynamic object:
dynamic d = JsonConvert.DeserializeObject<dynamic>(jsonString);
You'll get what you want.
You may use Microsoft Roslyn (here's the all-in-one NuGet package):
class Program
{
static void Main(string[] args)
{
string str = "a=1,b=2,c=3,d=\"4=four\"";
string script = String.Format("new {{ {0} }}",str);
var engine = new ScriptEngine();
dynamic d = engine.CreateSession().Execute(script);
}
}
And if you want to add even more complex types:
string str = "a=1,b=2,c=3,d=\"4=four\",e=Guid.NewGuid()";
...
engine.AddReference(typeof(System.Guid).Assembly);
engine.ImportNamespace("System");
...
dynamic d = engine.CreateSession().Execute(script);
Based on the question in your comment, there are code injection vulnerabilities. Add the System reference and namespace as shown right above, then replace the str with:
string str =
#" a=1, oops = (new Func<int>(() => {
Console.WriteLine(
""Security incident!!! User {0}\\{1} exposed "",
Environment.UserDomainName,
Environment.UserName);
return 1;
})).Invoke() ";
The question you described is something like deserialization, that is, contructing objects from data form(like string, byte array, stream, etc). Hope this link helps: http://msdn.microsoft.com/en-us/library/vstudio/ms233843.aspx
Here's a solution using ExpandoObject to store it after parsing it yourself. Right now it adds all values as strings, but you could add some parsing to try to turn it into a double, int, or long (you'd probably want to try it in that order).
static dynamic Parse(string str)
{
IDictionary<String, Object> obj = new ExpandoObject();
foreach (var assignment in str.Split(','))
{
var sections = assignment.Split('=');
obj.Add(sections[0], sections[1]);
}
return obj;
}
Use it like:
dynamic d = Parse("a=1,b=2,c=3");
// d.a is "1"