Delphi 10 Seattle로 애플리케이션을 개발 중입니다.
JSON에서 항목 값을 읽으려고합니다.
예 :
{
"findCompletedItemsResponse":[
{
"ack":[
"Success"
],
"version":[
"1.13.0"
],
"timestamp":[
"2016-06-02T16:07:36.736Z"
],
"searchResult":[
{
"@count":"2",
"item":[
{
"itemId":[
"172168793372"
],
"title":[
"Nikon D5000 12.3 MP Digital SLR Camera (Body Only with Accessories)"
],
"globalId":[
"EBAY-US"
],
"primaryCategory":[
{
"categoryId":[
"31388"
],
"categoryName":[
"Digital Cameras"
]
}
],
"galleryURL":[
"http:\/\/thumbs1.ebaystatic.com\/m\/mlRCNAriHPzowbSV9Q7ZFAg\/140.jpg"
],
"viewItemURL":[
"http:\/\/www.ebay.com\/itm\/Nikon-D5000-12-3-MP-Digital-SLR-Camera-Body-Only-Accessories-\/172168793372"
],
"paymentMethod":[
"PayPal"
],
"autoPay":[
"false"
],
"postalCode":[
"02806"
],
"location":[
"Barrington,RI,USA"
],
"country":[
"US"
],
"shippingInfo":[
{
"shippingServiceCost":[
{
"@currencyId":"USD",
"__value__":"0.0"
}
],
"shippingType":[
"Free"
],
"shipToLocations":[
"US"
],
"expeditedShipping":[
"true"
],
"oneDayShippingAvailable":[
"false"
],
"handlingTime":[
"2"
]
}
],
"sellingStatus":[
{
"currentPrice":[
{
"@currencyId":"USD",
"__value__":"178.5"
}
],
"convertedCurrentPrice":[
{
"@currencyId":"USD",
"__value__":"178.5"
}
],
"bidCount":[
"13"
],
"sellingState":[
"EndedWithSales"
]
}
],
"listingInfo":[
{
"bestOfferEnabled":[
"false"
],
"buyItNowAvailable":[
"false"
],
"startTime":[
"2016-04-18T18:45:54.000Z"
],
"endTime":[
"2016-04-25T18:45:54.000Z"
],
"listingType":[
"Auction"
],
"gift":[
"false"
]
}
],
"returnsAccepted":[
"false"
],
"condition":[
{
"conditionId":[
"3000"
],
"conditionDisplayName":[
"Used"
]
}
],
"isMultiVariationListing":[
"false"
],
"topRatedListing":[
"false"
]
},
{
"itemId":[
"172200026135"
],
"title":[
"Nikon D5000 12.3 MP Digital SLR Camera (Body Only with Accessories)"
],
"globalId":[
"EBAY-US"
],
"primaryCategory":[
{
"categoryId":[
"31388"
],
"categoryName":[
"Digital Cameras"
]
}
],
"galleryURL":[
"http:\/\/thumbs4.ebaystatic.com\/m\/mlRCNAriHPzowbSV9Q7ZFAg\/140.jpg"
],
"viewItemURL":[
"http:\/\/www.ebay.com\/itm\/Nikon-D5000-12-3-MP-Digital-SLR-Camera-Body-Only-Accessories-\/172200026135"
],
"paymentMethod":[
"PayPal"
],
"autoPay":[
"false"
],
"postalCode":[
"02806"
],
"location":[
"Barrington,RI,USA"
],
"country":[
"US"
],
"shippingInfo":[
{
"shippingServiceCost":[
{
"@currencyId":"USD",
"__value__":"0.0"
}
],
"shippingType":[
"Free"
],
"shipToLocations":[
"US"
],
"expeditedShipping":[
"true"
],
"oneDayShippingAvailable":[
"false"
],
"handlingTime":[
"2"
]
}
],
"sellingStatus":[
{
"currentPrice":[
{
"@currencyId":"USD",
"__value__":"119.49"
}
],
"convertedCurrentPrice":[
{
"@currencyId":"USD",
"__value__":"119.49"
}
],
"bidCount":[
"2"
],
"sellingState":[
"EndedWithSales"
]
}
],
"listingInfo":[
{
"bestOfferEnabled":[
"false"
],
"buyItNowAvailable":[
"false"
],
"startTime":[
"2016-05-10T07:22:34.000Z"
],
"endTime":[
"2016-05-16T19:22:25.000Z"
],
"listingType":[
"Auction"
],
"gift":[
"false"
]
}
],
"returnsAccepted":[
"false"
],
"condition":[
{
"conditionId":[
"3000"
],
"conditionDisplayName":[
"Used"
]
}
],
"isMultiVariationListing":[
"false"
],
"topRatedListing":[
"false"
]
}
]
}
],
"paginationOutput":[
{
"pageNumber":[
"1"
],
"entriesPerPage":[
"100"
],
"totalPages":[
"1"
],
"totalEntries":[
"2"
]
}
]
}
]
}
이베이에 나열된 항목의 가격 만 추출하고 싶습니다. 나는 currentPrice
.
가격의 가치 만 변수로 추출하려면 어떻게해야합니까?
이것은 끔찍한 JSON입니다. 이 데이터를 디자인 한 사람은 JSON을 전혀 이해하지 못합니다. 이 JSON은 요소가 1 개인 배열과 문자열 값을 지나치게 많이 사용합니다. 대부분의 배열은 전혀 속하지 않으며 JSON에는 다른 데이터 유형 (부울, 정수 등)을 사용할 수 있습니다.
어떤 경우 든 Delphi의 내장 JSON 프레임 워크 를 사용하여이 JSON을 구문 분석 할 수 있습니다 . 예 :
uses
System.JSON;
var
json: string;
obj: TJSONObject;
completedItems, sresults, items, status, price: TJSONArray;
I, J, K: Integer;
currencyId, value: string;
begin
json := ...; // <-- your JSON string here
obj := TJSONObject.ParseJSONValue(json, 0) as TJSONObject;
try
completedItems := obj.Values['findCompletedItemsResponse'] as TJSONArray;
for I := 0 to completedItems.Count-1 do
begin
sresults := (completedItems.Items[I] as TJSONObject).Values['searchResult'] as TJSONArray;
for J := 0 to sresults.Count-1 do
begin
items := (sresults.Items[J] as TJSONObject).Values['item'] as TJSONArray;
for K := 0 to items.Count-1 do
begin
status := (items.Items[K] as TJSONObject).Values['sellingStatus'] as TJSONArray;
price := ((status.Items[0] as TJSONObject).Values['currentPrice']) as TJSONArray;
currencyId := price.Values['@currencyId'].Value;
value := price.Values['__value__'].Value;
// use price values as needed...
end;
end;
finally
obj.Free;
end;
end;
또는 :
uses
System.JSON, System.JSON.Types;
var
json: string;
sreader: TStringReader;
jreader: TJsonTextReader;
inCurrentPrice: Boolean;
currencyId, value: string;
begin
json := ...; // <-- your JSON string here
sreader := TStringReader.Create(json);
try
jreader := TJsonTextReader.Create(sreader);
try
inCurrentPrice := False;
while jreader.Read do
begin
case jreader.TokenType of
TJsonToken.PropertyName: begin
if inCurrentPrice then
begin
if jreader.Value.AsString = 'currencyId' then begin
currencyId := jreader.ReadAsString;
end
else if jreader.Value.AsString = '__value__' then begin
value := jreader.ReadAsString;
end;
end
else if jreader.Value.AsString = 'currentPrice' then
begin
currencyId := '';
value := '';
inCurrentPrice := True;
end;
end;
TJsonToken.EndArray: begin
if inCurrentPrice then
begin
inCurrentPrice := False;
// use currency values as needed...
end;
end;
end;
end;
finally
jreader.Free;
end;
finally
sreader.Free;
end;
end;
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다