首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >delphi用System.JSON解析jsonarray

delphi用System.JSON解析jsonarray
EN

Stack Overflow用户
提问于 2016-01-09 05:46:00
回答 1查看 10.7K关注 0票数 1

使用Delphi 8的System.JSON,我不能解析jsonarray。

样品Json

代码语言:javascript
复制
{
    "data":{
        "current_condition":[
            {
                "cloudcover":"0",
                "FeelsLikeC":"-9",
                "FeelsLikeF":"15",
                "humidity":"93",
                "observation_time":"04:10 AM",
                "precipMM":"0.0",
                "pressure":"1007",
                "temp_C":"-6",
                "temp_F":"21",
                "visibility":"10",
                "weatherCode":"113",
                "weatherDesc":[
                    
                ],
                "weatherIconUrl":[
                    
                ],
                "winddir16Point":"SE",
                "winddirDegree":"130",
                "windspeedKmph":"7",
                "windspeedMiles":"4"
            }
        ]
    }
}

使用代码:

代码语言:javascript
复制
memores: TStringList;
currcond: TJSONObject;

memores2.Text := http.Get ('url');
JSONObject := TJSONObject.ParseJSONValue(memores2.Text) as TJSONObject;
Memores1.add('current_condition');
JSONObject2 := JSONObject.GetValue('data')   as TJSONObject;
arrayjson := JSONObject2.ParseJSONValue('current_condition')  as TJSONArray;
currcond := arrayjson.Items[0] as TJSONObject;
memores1.Add((currcond.GetValue('cloudcover').Value));
EN

回答 1

Stack Overflow用户

发布于 2021-04-06 09:50:53

作为提供的解决方案的替代方案,但使用mORMot

代码语言:javascript
复制
{$APPTYPE CONSOLE}

uses
  SynCommons;

const
    Text =  '{ "data":{ "current_condition":[ { "cloudcover":"0", "FeelsLikeC":"-9", "FeelsLikeF":"15", "humidity":"93", ' +
            '"observation_time":"04:10 AM", "precipMM":"0.0", "pressure":"1007", "temp_C":"-6", "temp_F":"21", "visibility":"10", ' +
            '"weatherCode":"113", "weatherDesc":[], "weatherIconUrl":[], "winddir16Point":"SE", "winddirDegree":"130", "windspeedKmph":"7", "windspeedMiles":"4" } ] } }';

var Data , CurrentConditionArray : TDocVariantData;
    Current_Condition , Json : Variant;
begin
  Json := _Json(Text);
  // Alternative 1
  Current_Condition := Json.data.current_condition._(0);
  Assert( Current_Condition.observation_time = '04:10 AM' );

  // Alternative 2 slightly faster
  Data := TDocVariantData(Json).O['data']^;
  CurrentConditionArray := Data.A['current_condition']^;
  Current_Condition := CurrentConditionArray.Values[0];
  Assert( TDocVariantData(Current_Condition).Value['precipMM'] = '0.0' );
end.

这应该从Delphi 7到10.4起作用。请在惊人的文档中找到更多的细节和替代方案

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34690150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档