1

I'm not allowed to import new packages at the customer. I'm only allowed in the area between obtaining a String object looking like a JSON string and a return, where I'm supposed to return some parts of it in a List<String>.

I feel terribly limited and as far I can see, I can't proceed. My best bet is using Regex object but perhaps there's a smoother solution? (I believe I'm allowed to use XDocument too, and LINQ, if that's of any help).

Suggestions?

13
  • 5
    It looks like JSON or it is actual JSON? Commented Feb 8, 2013 at 13:40
  • 1
    There are plenty of open source libraries that'll parse it for you. You can import them as code rather then as a dll. Commented Feb 8, 2013 at 13:43
  • What a ridiculous constraint. Looks like you'll need to learn a bit of parsing theory. Commented Feb 8, 2013 at 13:43
  • Could you give a example of the string? Commented Feb 8, 2013 at 14:51
  • @MatíasFidemraizer It's not an object. It's just a string. It's received from a web service that distributes JSON objects. Make you judgement. :) Commented Feb 8, 2013 at 15:01

1 Answer 1

1

Follow this manual and you should be able to do it.

You only need to import System.Runtime.Serialization.dll (standerd .net dll)

edit:

You have this method

public static T JsonDeserialize<T> (string jsonString)

If you know what json you will get you can create object like this:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

So you can use it like this:

string jsonString = "{\"Age\":28,\"Name\":\"Tom\"}";
Person p = JsonHelper.JsonDeserialize<person>(jsonString);

edit2:

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    static void Main(string[] args)
    {
        string json = "[{\"Age\":28,\"Name\":\"Tom\"},{\"Age\":18,\"Name\":\"Andes\"},{\"Age\":32,\"Name\":\"Lily\"}]";
        List<Person> persons = new List<Person>(JsonHelper.JsonDeserialize<Person[]>(json));

    }

Edit 3: JsonHelper is a class implemting a default settup.

/// <summary>
/// JSON Serialization and Deserialization Assistant Class
/// </summary>
public class JsonHelper
{
    /// <summary>
    /// JSON Serialization
    /// </summary>
    public static string JsonSerializer<T>(T t)
    {
        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
        MemoryStream ms = new MemoryStream();
        ser.WriteObject(ms, t);
        string jsonString = Encoding.UTF8.GetString(ms.ToArray());
        ms.Close();
        return jsonString;
    }
    /// <summary>
    /// JSON Deserialization
    /// </summary>
    public static T JsonDeserialize<T>(string jsonString)
    {
        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
        MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
        T obj = (T)ser.ReadObject(ms);
        return obj;
    }
}
Sign up to request clarification or add additional context in comments.

15 Comments

you make the assumption that .net is allowed- i work with customers who do not allow it at all
@TomBeech well since it is tagged C# I assumed that yes
Suppose they'll let me do that. Then what? I've checked on my computer and there's a factory that I'll need to use to get a parser but I'm not clear on how to get that to convert the string to anything useful.
@AndreasJohansson what do you mean with "there's a factory that I'll need to use to get a parser"?
I was looking into what's in the System.Runtime.Serialization.Json namespace. Also, I don't seem to be able to find JsonHelper in any namespace. Where do you find it?
|