How to simulate JSON responses?

Asked

Viewed 993 times

6

I need something that simulates JSON responses for me. A while ago I had seen a site that did the following:

  • You pass parameters through a URL, e.g.: ?nome=Wesley&idade=21
  • Received in the call the JSON object like this:

     {
         nome:"Wesley",
         idade : 21
     }
    

But I can’t find it, which would be something that does something similar?

  • 2

    what you’re looking for is the site that does it? or how to turn that query into an object?

3 answers

7


If you are looking for a service you pass some parameters and it returns in format JSON?

I believe it’s something similar to that website.

Where that call: http://echo.jsontest.com/key/value/one/two

There’s that return:

{
   "one": "two",
   "key": "value"
}

Check if it suits you. Although not accept QueryString, as requested in the question.

  • 2

    The only small inconvenience is that the service does not accept the parameters in the format of a querystring as demonstrated in the author of the topic. But this is minimal, since because there is an API just prepare the data before consuming.

  • @Brunoaugusto, yes, I even thought one would be found in the format that the author requested and I looked, to save in my favorites, for when you need. But for now nothing. hehe

5

0

Basically what you need is to serialize your collection of QueryStrings.

In C#, you can create a Handler with the following content:

public void ProcessRequest(HttpContext context)
{
    string jsonContent = JsonConvert.SerializeObject(context.Request.QueryString);
    context.Response.ContentType = "text/plain";
    context.Response.Write(jsonContent);
}

All content passed to this Handler will be returned in JSON format, just like in your example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.