Инструменты пользователя

Инструменты сайта


blackbox:webjson

https://gist.github.com/hodzanassredin/f86b1536ea947410b239132b68728851

или так

MODULE DemoParsingJson;
	IMPORT Log, Files, HttpClient, AosStreams, DS := StringsDyn, M := YSonModels, YSonConverters;
	TYPE Handler = PROCEDURE (f: Files.File; data: ARRAY OF CHAR);

	PROCEDURE Parse (stream: AosStreams.Reader);
	VAR ch: SHORTCHAR; buf: DS.DynString; v0: M.Value; v, item: M.Value;  res: INTEGER;
	BEGIN
		buf := DS.Create(""); WHILE stream.GetChar(ch) DO buf.AddChar(ch) END; Log.Ln; Log.Ln;
		v0 := YSonConverters.Load(buf, res);
		IF v0 # NIL THEN
			WITH v0: M.Holder DO
				v := v0.First();
				WHILE v # NIL DO
					WITH v: M.Object DO
						item := v.Find("created_at");
						IF item # NIL THEN
							WITH item: M.String DO Log.String("created at: " + item.Get()); Log.Ln ELSE END
						END;
						item := v.Find("state");
						IF item # NIL THEN
							WITH item: M.String DO Log.String("state: " + item.Get()); Log.Ln ELSE END
						END;
						item := v.Find("url");
						IF item # NIL THEN
							WITH item: M.String DO Log.String("url: " + item.Get()); Log.Ln; Log.Ln ELSE END
						END;
					ELSE END;
					v := v0.Next(v)
				END
			ELSE END
		ELSE Log.String("Не могу распарсить... Код ошибки: "); Log.Int(res); Log.Ln END
	END Parse;

	PROCEDURE Start*;
	VAR client: HttpClient.HTTPConnection; r: AosStreams.Reader; res: INTEGER;
	BEGIN
		NEW(client); client.New();
		client.Get("https://api.github.com/repos/facebook/react/issues", TRUE, r, res);
		IF res = HttpClient.Ok THEN
			Parse(r)
		ELSE Log.Int(client.responseHeader.statuscode);
			Log.String(LONG(client.responseHeader.reasonphrase)); Log.Ln;
		END;
	END Start;
END DemoParsingJson.
blackbox/webjson.txt · Последнее изменение: 2024/06/25 16:07 — iadenisov