how can i turn this JSON to a list of objects that have two properties first "id" and second "answer" ?
[["9","3"],["8","4"],["7","4"],["6","5"],["5","6"],["4","4"],["3","4"]]
how can i turn this JSON to a list of objects that have two properties first "id" and second "answer" ?
[["9","3"],["8","4"],["7","4"],["6","5"],["5","6"],["4","4"],["3","4"]]
There's 101 ways but here's a .Net 2.0 string parse method:
Dictionary<int, int> jsonValues = new Dictionary<int, int>();
string data = "[[\"9\",\"3\"],[\"8\",\"4\"],[\"7\",\"4\"],[\"6\",\"5\"],[\"5\",\"6\"],[\"4\",\"4\"],[\"3\",\"4\"]]";
string[] items = data.Split(new string[]{"\"],[\""}, StringSplitOptions.RemoveEmptyEntries);
foreach (string str in items)
{
string[] intVals = str
.Replace("\"", "")
.Replace("[", "")
.Replace("[", "")
.Replace("]", "").Split(',');
jsonValues.Add(int.Parse(intVals[0]), int.Parse(intVals[1]));
}
// test print:
foreach (KeyValuePair<int,int> kvp in jsonValues)
{
System.Diagnostics.Debug.WriteLine(
"ID:" + kvp.Key + " val:" + kvp.Value );
}
by the way. Since you're extracting name value pairs i just used a dictionary of int/int to hold the data.
Here is a solution using RegEx
string data = "[[\"9\",\"3\"],[\"8\",\"4\"],[\"7\",\"4\"],[\"6\",\"5\"],[\"5\",\"6\"],[\"4\",\"4\"],[\"3\",\"4\"]]";
var pairs = (from Match m in Regex.Matches(data, "\"[0-9]\"\\,\"[0-9]\"")
select m.Value.Split(',')).ToDictionary(d => d[0], d => d[1]);
Added: if you want the resulting values as int instead of strings then you can do this instead
var pairs = (from Match m in Regex.Matches(data, "\"[0-9]\"\\,\"[0-9]\"")
select m.Value.Split(','))
.ToDictionary(d => Int32.Parse(d[0].Replace("\"", "")),
d => Int32.Parse(d[1].Replace("\"", "")));
I started using JSON.NET - http://james.newtonking.com/pages/json-net.aspx . Its a very comprehensive JSON library that allows you to do pretty much anything.
In Silverlight, there's System.Json.dll that makes JSON parsing very easy. There was some talk of getting it into .NET4.0, not sure if it happened.
In regular .NET, you can use the DataContractJsonSerializer (use something like List> as the "type"). This works in .NET3.0 and above. May not be the best choice for all JSON parsing scenarios (but will work for yours).
a very literal answer. Will only work for the exact format you specified, may go a little haywire if you pass it unexpected data. Returns a KeyValuepair with each pair in it.
var val = "[[\"9\",\"3\"],[\"8\",\"4\"],[\"7\",\"4\"],[\"6\",\"5\"],[\"5\",\"6\"],[\"4\",\"4\"],[\"3\",\"4\"]]";
var result = val.ToCharArray()
.Where(itm => Char.IsDigit(itm))
.Select((itm, index) => new {c = int.Parse(itm.ToString()),index = index + 1})
.GroupBy(itm => itm.index % 2 == 0 ? itm.index - 1 : itm.index)
.Select(itm => new KeyValuePair<int, int>(itm.ElementAt(0).c, itm.ElementAt(1).c));
Requires reference to System.Web.Extensions
assembly;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Web.Script.Serialization;
class Program
{
public class Test
{
public string Id { get; set; }
public string Answer { get; set; }
}
static void Main(string[] args)
{
string data ="[[\"9\",\"3\"],[\"8\",\"4\"],[\"7\",\"4\"],[\"6\",\"5\"]]";
List<Test> tests =
Array.ConvertAll<ArrayList, Test>(
new JavaScriptSerializer()
.Deserialize<ArrayList>(data)
.OfType<ArrayList>().ToArray(),
(item) =>
{
return new Test()
{
Id = (string)item[0],
Answer = (string) item[1]
};
}).ToList();
}
}
ROFL, HTH
I think the easyiest way to the job is the following piece of code:
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
namespace JsonParser
{
class Program
{
static void Main(string[] args)
{
string data = "[[\"9\",\"3\"],[\"8\",\"4\"],[\"7\",\"4\"],[\"6\",\"5\"],[\"5\",\"6\"],[\"4\",\"4\"],[\"3\",\"4\"]]";
var stream = new MemoryStream(new ASCIIEncoding().GetBytes(data));
var deserializer = new DataContractJsonSerializer(typeof(List<List<string>>));
var result = (List<List<string>>)deserializer.ReadObject(stream);
}
}
}
of course result contains a "List>" which is the correct type of your Json string. Also you have to remember to add references to the following dlls: