0

I have this json string that looks like:

string jsonString = "[ {"id": "1"}, {"id": "2", "category": "toys"} ]";

The quotations are already escaped in the string. And I want to find a nice way to create a link out of json that looks similar to this by converting this to GET parameters. I have only really seen solutions that work well for flat structures.

Edit: I also need tp be able to convert back into the json string.

2
  • What object is your jsonString representing? Are you doing a complex GET of something from the server that requires three query string parameters? Commented Jun 11, 2015 at 8:07
  • I want to convert this string (which was a json cookie I url-decoded) and I want to attach it to the end of a link I'm making so I can email it. Then when the user clicks the link, I convert back to json and process the data. Commented Jun 11, 2015 at 8:17

2 Answers 2

1

Initial Answer

You could do something like this to turn it into a link:

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;

public class Program
{
    public static void Main()
    {       
        dynamic obj = JArray.Parse(jsonString);

        var builder = new StringBuilder();
        builder.Append("?id0=" + obj[0].id);
        builder.Append("&id1=" + obj[1].id);
        builder.Append("&category1=" +obj[1].category);

        Console.WriteLine("http://www.something.com" + builder.ToString());
    }

    public static string jsonString = @"[ {""id"": ""1""}, {""id"": ""2"", ""category"": ""toys""} ]";
}

Output:

http://www.something.com?id0=1&id1=2&category1=toys

More Generic Follow Up Answer

Based on your comment, here is something more generic:

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        JArray array = JArray.Parse(jsonString);

        var builder = new StringBuilder();

        for (var i = 0; i < array.Count; ++i)
        {           
            JToken obj = array[i];
            foreach (JProperty prop in obj)
            {
                var prefix = i == 0 ? "?" : "&";
                builder.AppendFormat("{0}{1}{2}={3}", prefix, prop.Name, i, prop.Value);
            }
        }

        Console.WriteLine("http://www.something.com" + builder.ToString());
    }

    public static string jsonString = @"[ {""id"": ""1""}, {""id"": ""2"", ""category"": ""toys""} ]";
}

Output:

http://www.something.com?id0=1&id1=2&category1=toys

Sign up to request clarification or add additional context in comments.

4 Comments

Is there a solution that would convert into get params regardless of the content structure?
@SatbirKira Yes. There is a solution. I added something to my answer, though it might not be quite as generic as what you require. It really depends on the cases that you need to handle. Does it need to handle every possible JSON structure? I.e. arrays nested in objects nested in arrays, etc.
I think this works great. I just need to be able to convert back into a json string now from the string.
Maybe I am asking this question in the wrong way, I have this json string serialized and I want to email a link that contains this serialized link. I figured get parameters was the best way.
0

Okay to answer my own question, this isn't a good way to solve the problem. The json string can be very large and may be truncated or denied if over 2048 characters on the server.

The best approach is to create a jsonHashTable.json that will store the hash of what you are trying to send as the key, and the thing you are sending as a value. Then email the hash/key. Have the controller receiving the hash use the table to look up what data was needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.