unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, IdBaseComponent,
IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, DBXJSON, System.JSON;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
IdHTTP1: TIdHTTP;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const Url = 'url';
function JSONArrayCovertCnt(usJSON: string): integer;
var
JSONPair : TJSONPair;
JSONArray : TJSONArray;
begin
usJson := StringReplace(usJSON,'\"','"',[rfReplaceAll]);
JSONArray := TJSONObject.ParseJSONValue(usJSON) as TJSONArray;
Result := JSONArray.Count;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
stream: TStringStream; idHttpObj: TIdHTTP;
JSONValue : TJSONvalue;
i : integer;
JSONArray : TJSONArray;
result : integer;
begin
stream := TStringStream.Create('', TEncoding.UTF8); //
idHttpObj := TIdHTTP.Create(nil);
idHttpObj.Get(Url, stream);
idHttpObj.Free;
JSONValue := TJSONObject.ParseJSONValue(stream.DataString);
memo1.Clear;
for i := 0 to 3 do
begin
Memo1.Lines.Add('year: ' JSONValue.GetValue<string>('data[' i.ToString '].year') 'year');
Memo1.Lines.Add('hshld: ' JSONValue.GetValue<string>('data[' i.ToString '].hshld') 'hshld');
Memo1.Lines.Add('popltn: ' JSONValue.GetValue<string>('data[' i.ToString '].popltn_sm') '?');
Memo1.Lines.Add('popltn_male: ' JSONValue.GetValue<string>('data[' i.ToString '].popltn_male') '?');
Memo1.Lines.Add('popltn_female: ' JSONValue.GetValue<string>('data[' i.ToString '].popltn_female') '?');
Memo1.Lines.Add('hshld_avrgpopltn: ' JSONValue.GetValue<string>('data[' i.ToString '].hshld_avrgpopltn') '?');
Memo1.Lines.Add('────────────────────────────────────');
end;
Memo1.Lines.Add('resultCode: ' JSONValue.GetValue<string>('resultCode'));
Memo1.Lines.Add('resultMsg: ' JSONValue.GetValue<string>('resultMsg'));
Memo1.Lines.Add('numOfRows : ' JSONValue.GetValue<string>('numOfRows'));
Memo1.Lines.Add('resultCode: ' JSONValue.GetValue<string>('resultCode'));
Memo1.Lines.Add('totalcount: ' JSONValue.GetValue<string>('totalCount'));
result := JSONArrayCovertCnt(stream.DataString);
edit1.Text := IntToStr(result);
stream.Free;
end;
end.
{
"data":
[
{
"popltn_female":134301,
"year":"2012",
"hshld_avrgpopltn":2.65,
"hshld":102031,
"popltn_sm":270460,
"popltn_male":136159
}
]
"pageNo":1,
"currentCount":9,
"resultCode":0,
"totalCount":9,
"numOfRows":10,
"resultMsg":"??"
}
我想計算 JSONValue 中有多少資料陣列值。
我嘗試通過 Google 搜索查找、輸入和應用功能代碼,但出現錯誤。
無效的型別別轉換
我們如何解決這個問題?
我用翻譯問了一個問題,因為我是一個完全不擅長英語的韓國人。
uj5u.com熱心網友回復:
您顯示的 JSON 資料表示一個物件(由 表示{ }),而不是一個陣列(由 表示[ ]),因此ParseJSONValue()將回傳TJSONValue指向 a 的 a TJSONObject,而不是 a TJSONArray。因此,as型別轉換TJSONArray將失敗并出現運行時錯誤。JSON 物件內的data欄位是一個陣列。
附帶說明一下,您的代碼還有其他問題:
您的使用
StringReplace()是完全不必要的,應該洗掉。您的 JSON 不包含任何'\'字符,但即使包含,從 JSON 中洗掉任何內容都是錯誤的。您需要完全按照收到的方式決議它。您的所有
Create/Free對都需要用try/保護finally。您正在泄漏回傳的兩個
TJSONValue物件ParseJSONValue()。你根本不需要
TStringStream,因為TIdHTTP.Get()有一個回傳 a 的多載String。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/503929.html
