Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Http get json into string

I'm trying to get json response via HTTP GET method to a string but all I got is that: enter image description here

I'm using a code like that:

memo1.Text:= idhttp1.Get('http://blabla.com/bla.php');

it returns json data. i need to get that json response to memo1.

How can I do that?

like image 549
Someone Avatar asked Mar 27 '13 03:03

Someone


People also ask

How do I get response JSON?

To return JSON from the server, you must include the JSON data in the body of the HTTP response message and provide a "Content-Type: application/json" response header. The Content-Type response header allows the client to interpret the data in the response body correctly.

What is JsonArray?

JsonArray represents an immutable JSON array (an ordered sequence of zero or more values). It also provides an unmodifiable list view of the values in the array. A JsonArray object can be created by reading JSON data from an input source or it can be built from scratch using an array builder object.

What is JSON parsing?

JSON parsing is the process of converting a JSON object in text format to a Javascript object that can be used inside a program. In Javascript, the standard way to do this is by using the method JSON.

Can you send JSON in header?

This means when you're sending JSON to the server or receiving JSON from the server, you should always declare the Content-Type of the header as application/json as this is the standard that the client and server understand.


2 Answers

I found a solution. That code works perfect.

function GetURLAsString(aURL: string): string;
var
  lHTTP: TIdHTTP;
  lStream: TStringStream;
begin
  lHTTP := TIdHTTP.Create(nil);
  lStream := TStringStream.Create(Result);
  try
    lHTTP.Get(aURL, lStream);
    lStream.Position := 0;
    Result := lStream.ReadString(lStream.Size);
  finally
    FreeAndNil(lHTTP);
    FreeAndNil(lStream);
  end;
end;
like image 124
Someone Avatar answered Sep 28 '22 17:09

Someone


Try a newer version of Indy, Indy still supports non-Unicode Delphi.

The Indy TIdHTTP Get should automatially convert UTF-8 to Ansi (with limitations):

Internally, TIdHTTP.Get() decodes the server's UTF-8 data to Unicode, and then converts that to Ansi when returning it as an AnsiString in pre-2009 versions. That AnsiString uses the OS's default Ansi codepage, so yes, there is potential for data loss if the OS's current language does not support the Unicode characters being used by the server. http://forums2.atozed.com/viewtopic.php?f=7&t=3011

like image 37
mjn Avatar answered Sep 28 '22 17:09

mjn