Что нового

[Данные, строки] Создание из Json'a массив с данными

Kasper

Знающий
Сообщения
70
Репутация
8
Здравствуйте. У меня есть Json и мне надо из него сделать массив
Код:
{
	"response": {
		"game_count": 44,
		"games": [
			{
				"appid": 240,
				"name": "Counter-Strike: Source",
				"playtime_2weeks": 25,
				"playtime_forever": 23091,
				"img_icon_url": "9052fa60c496a1c03383b27687ec50f4bf0f0e10",
				"img_logo_url": "ee97d0dbf3e5d5d59e69dc20b98ed9dc8cad5283",
				"has_community_visible_stats": true
			},
			{
				"appid": 300,
				"name": "Day of Defeat: Source",
				"playtime_forever": 72,
				"img_icon_url": "062754bb5853b0534283ae27dc5d58200692b22d",
				"img_logo_url": "e3a4313690bd551495a88e1c01951eb26cec7611",
				"has_community_visible_stats": true
			},
			{
				"appid": 320,
				"name": "Half-Life 2: Deathmatch",
				"playtime_forever": 0,
				"img_icon_url": "795e85364189511f4990861b578084deef086cb1",
				"img_logo_url": "6dd9f66771300f2252d411e50739a1ceae9e5b30",
				"has_community_visible_stats": true
			},
			{
				"appid": 340,
				"name": "Half-Life 2: Lost Coast",
				"playtime_forever": 0,
				"img_icon_url": "795e85364189511f4990861b578084deef086cb1",
				"img_logo_url": "867cce5c4f37d5ed4aeffb57c60e220ddffe4134"
			},
			{
				"appid": 4000,
				"name": "Garry's Mod",
				"playtime_forever": 338,
				"img_icon_url": "d9101cbeddcc4ff06c7fa1936c3f381b0bbf2e92",
				"img_logo_url": "dca12980667e32ab072d79f5dbe91884056a03a2",
				"has_community_visible_stats": true
			},
			{
				"appid": 6840,
				"name": "Commandos 3: Destination Berlin",
				"playtime_forever": 0,
				"img_icon_url": "73f9a99ca805232d3fd632fc2eba4ad6e2832195",
				"img_logo_url": "6df824b0b03ae63687853825a3e276c828fc9854"
			},
			{
				"appid": 46410,
				"name": "Avencast",
				"playtime_forever": 0,
				"img_icon_url": "8115df3238c9d1176f516cea23fe3fc9846f5b4b",
				"img_logo_url": "bc66feb72ed8c32d72b7670b4d5e26f3bd1291d1"
			},
			{
				"appid": 43110,
				"name": "Metro 2033",
				"playtime_forever": 15,
				"img_icon_url": "a70fe6dc214f24107d20596f3040dbfa220ed42d",
				"img_logo_url": "df9a163ac1f28dfc84c93a6fc0dc51719eaef518",
				"has_community_visible_stats": true
			},
			{
				"appid": 104200,
				"name": "BEEP",
				"playtime_forever": 89,
				"img_icon_url": "a08809b6b78b2adcc347ad406d4deb2946761e4b",
				"img_logo_url": "26fed500a3e0c4765a73a40f53ac24b0c84f8d43"
			},
			{
				"appid": 22230,
				"name": "Rock of Ages",
				"playtime_forever": 191,
				"img_icon_url": "775c7ad571b7571aec5a85235759fbc2baa12eee",
				"img_logo_url": "4df41d2c1847cf8cba4d9603ff1e36a5a9b77fc9",
				"has_community_visible_stats": true
			},
			{
				"appid": 96100,
				"name": "Defy Gravity",
				"playtime_forever": 13,
				"img_icon_url": "fa26f1090915f4248bdabe9c99e3d51113fb6e3f",
				"img_logo_url": "1533e9bf83bfd47553791f9b60500d33e8364d38",
				"has_community_visible_stats": true
			},
			{
				"appid": 730,
				"name": "Counter-Strike: Global Offensive",
				"playtime_2weeks": 561,
				"playtime_forever": 8810,
				"img_icon_url": "69f7ebe2735c366c65c0b33dae00e12dc40edbe4",
				"img_logo_url": "d0595ff02f5c79fd19b06f4d6165c3fda2372820",
				"has_community_visible_stats": true
			},
			{
				"appid": 104700,
				"name": "Super Monday Night Combat",
				"playtime_forever": 0,
				"img_icon_url": "5a5dd4420068a68f0d654edbfabf2bff5f8b3b0a",
				"img_logo_url": "6c57677925f8b3658e315d3e40c1f928a7653ddf",
				"has_community_visible_stats": true
			},
			{
				"appid": 208090,
				"name": "Loadout",
				"playtime_forever": 4,
				"img_icon_url": "d4dc7a4f71d4ba7c44cd05b5c235e0a387ca212b",
				"img_logo_url": "bd03747de2f7380bd779b9a355790522b85615b2",
				"has_community_visible_stats": true
			},
			{
				"appid": 218130,
				"name": "Dungeonland",
				"playtime_forever": 0,
				"img_icon_url": "9a0cb15760c9c744b7ef96ecc5effff22136c878",
				"img_logo_url": "80fa1305a0eb4c3512cf2423be2716a0140d9a35",
				"has_community_visible_stats": true
			},
			{
				"appid": 236390,
				"name": "War Thunder",
				"playtime_forever": 0,
				"img_icon_url": "88259dcb7014dcdb109d9ece0cfc70c100bc2f9c",
				"img_logo_url": "219b91aa76b2c6325b763af5e4ad411fab535bf9",
				"has_community_visible_stats": true
			},
			{
				"appid": 271290,
				"name": "HAWKEN",
				"playtime_forever": 0,
				"img_icon_url": "c2ff4b2c2652a897935631b040c5ba135fe655e3",
				"img_logo_url": "f767633be94e6072d6b476040a1decfd6bbd93fa",
				"has_community_visible_stats": true
			},
			{
				"appid": 42160,
				"name": "War of the Roses",
				"playtime_forever": 0,
				"img_icon_url": "a87b69ac8257540cf378fe17dc9b6beaf7c15eee",
				"img_logo_url": "78a33bc66b2d52371ee1dc7d210c95167bd0c895",
				"has_community_visible_stats": true
			},
			{
				"appid": 245550,
				"name": "Free to Play",
				"playtime_forever": 0,
				"img_icon_url": "ff076c6bed6fe8cd7a919c500e54dc8eb8634ef2",
				"img_logo_url": "98defd2daedb3910583439bffb12e0baeb20ff6c",
				"has_community_visible_stats": true
			},
			{
				"appid": 246280,
				"name": "Happy Wars",
				"playtime_forever": 0,
				"img_icon_url": "5ee503b2d09fc074e723f09c8cb13a808fb153b4",
				"img_logo_url": "7a7706082a03aa96c3291187341ef7aa4ea0c941",
				"has_community_visible_stats": true
			},
			{
				"appid": 252150,
				"name": "Grimm",
				"playtime_forever": 0,
				"img_icon_url": "d9f47d9878d2242ac2b57491567eaa6290f13258",
				"img_logo_url": "33e422c461774efe02739b23aef8fd56a1df50f5"
			},
			{
				"appid": 235720,
				"name": "Cubetractor",
				"playtime_forever": 0,
				"img_icon_url": "ff4d47fdd283c9a6309704354741d199ae1a1617",
				"img_logo_url": "072780dcb2e87f6bd8028c8de9798523cbbf8371",
				"has_community_visible_stats": true
			},
			{
				"appid": 234390,
				"name": "Teleglitch: Die More Edition",
				"playtime_forever": 51,
				"img_icon_url": "555ec0ee9fb0ce13dc57450f2731b6b474ab11ee",
				"img_logo_url": "2aaffcf9578f1eace6a68b0623f04971a0572e8a",
				"has_community_visible_stats": true
			},
			{
				"appid": 218620,
				"name": "PAYDAY 2",
				"playtime_forever": 200,
				"img_icon_url": "a6abc0d0c1e79c0b5b0f5c8ab81ce9076a542414",
				"img_logo_url": "4467a70648f49a6b309b41b81b4531f9a20ed99d",
				"has_community_visible_stats": true
			},
			{
				"appid": 251970,
				"name": "Sins of a Dark Age",
				"playtime_forever": 14,
				"img_icon_url": "cee4aeadcf73b7b00018c535fa3b850bfcdb9982",
				"img_logo_url": "b0a998ce21d6fb7639d7d54acf0ed20975b7fbef",
				"has_community_visible_stats": true
			},
			{
				"appid": 253980,
				"name": "Enclave",
				"playtime_forever": 0,
				"img_icon_url": "eb807cb60bbb54f7103659b17ca74c88477fa8f1",
				"img_logo_url": "59fe92a2156284c5f9f84d4aae6da12eeede56fa"
			},
			{
				"appid": 254000,
				"name": "East India Company Gold",
				"playtime_forever": 0,
				"img_icon_url": "517c5f57b39abe6428aec9487d52ca987595e87e",
				"img_logo_url": "1d14ef0cd0564be38d421e70be755c33be1c6ace"
			},
			{
				"appid": 254060,
				"name": "KnightShift",
				"playtime_forever": 0,
				"img_icon_url": "b1fbbfcbb5f85058a10f96a9262946ffae0c5982",
				"img_logo_url": "6dec0d71e514c0aac7d23a7718094b7a099da08c",
				"has_community_visible_stats": true
			},
			{
				"appid": 282440,
				"name": "Quake Live",
				"playtime_forever": 0,
				"img_icon_url": "bac9828d3e193c948801b14660490576fbbf9f72",
				"img_logo_url": "bba7e836bc54e709020ee4d95c08f4dff1d23537"
			},
			{
				"appid": 285580,
				"name": "ACE - Arena: Cyber Evolution",
				"playtime_forever": 0,
				"img_icon_url": "25f21cf4daac2ea82a217f3688375ee76d94c624",
				"img_logo_url": "a631da96c600404a26c847f003e49230dd89b939",
				"has_community_visible_stats": true
			},
			{
				"appid": 291010,
				"name": "The Hat Man: Shadow Ward",
				"playtime_forever": 0,
				"img_icon_url": "8a9fd7a24e99762464daa999cde440cca5373d2c",
				"img_logo_url": "eb73de1295eb89e671c196df13de78200ac10db3",
				"has_community_visible_stats": true
			},
			{
				"appid": 292380,
				"name": "Racer 8",
				"playtime_forever": 0,
				"img_icon_url": "dc95e7d7c2a247a42e226733ebb229963b065454",
				"img_logo_url": "4c0577f37d5eaba1cc76c9607ddd6a983fc0e78c",
				"has_community_visible_stats": true
			},
			{
				"appid": 292390,
				"name": "Realms of the Haunting",
				"playtime_forever": 0,
				"img_icon_url": "13abd6b022bb179fe9fdc41411ac0c525c320639",
				"img_logo_url": "8a739324621874fa3e31b440f1712194c7674a7f"
			},
			{
				"appid": 297020,
				"name": "The Culling Of The Cows",
				"playtime_forever": 267,
				"img_icon_url": "b7903ebb375cc33c6e5ce3e64d3af5540e4973b3",
				"img_logo_url": "70ef9660e81a85a1d5d33faee9a13c9959098015",
				"has_community_visible_stats": true
			},
			{
				"appid": 301520,
				"name": "Robocraft",
				"playtime_forever": 1,
				"img_icon_url": "939ba382462f3816cf25bae02e3b5363904297d3",
				"img_logo_url": "18bfb1e863955ce5396392525a9c3efe0507c9b9"
			},
			{
				"appid": 304930,
				"name": "Unturned",
				"playtime_forever": 1,
				"img_icon_url": "e18009fb628b35953826efe47dc3be556b689f4c",
				"img_logo_url": "0a9bd2d303e6142edd098292026ee55e7dfeb821",
				"has_community_visible_stats": true
			},
			{
				"appid": 307170,
				"name": "Borealis",
				"playtime_forever": 745,
				"img_icon_url": "e53647be35688c6ab10729a49448f9d2c3e1601a",
				"img_logo_url": "461aff63208be12b54e3e5b1e38e43c68223397d",
				"has_community_visible_stats": true
			},
			{
				"appid": 227940,
				"name": "Heroes & Generals",
				"playtime_forever": 0,
				"img_icon_url": "fb3571b94567750b621f12eaea6108f73cb1e621",
				"img_logo_url": "abc7d3561497c3a75fa66658ae3ac401131dbf3f",
				"has_community_visible_stats": true
			},
			{
				"appid": 222900,
				"name": "Dead Island: Epidemic",
				"playtime_forever": 0,
				"img_icon_url": "eeca961216745840443d87373f907d1e6e4ca764",
				"img_logo_url": "e5b801707f7373d69eab3f15b6fdb02f7270374a",
				"has_community_visible_stats": true
			},
			{
				"appid": 200110,
				"name": "Nosgoth",
				"playtime_forever": 0,
				"img_icon_url": "5059f6d2b9c87f10a631fcdcbed1020f7aae0ef9",
				"img_logo_url": "9894c6c94dca1b2c6f1836a3c780d8504efdd4ef",
				"has_community_visible_stats": true
			},
			{
				"appid": 205790,
				"name": "Dota 2 Test",
				"playtime_forever": 0,
				"img_icon_url": "",
				"img_logo_url": ""
			},
			{
				"appid": 24240,
				"name": "PAYDAY: The Heist",
				"playtime_2weeks": 2,
				"playtime_forever": 867,
				"img_icon_url": "a07ceea8808f1a2104ed1c864756f263ec67df49",
				"img_logo_url": "985cce1caab10e5b5f11af73c75fe0c5411ed76a",
				"has_community_visible_stats": true
			},
			{
				"appid": 335430,
				"name": "Grimoire: Manastorm",
				"playtime_forever": 0,
				"img_icon_url": "9668d55b6801e2034ff9035c9b8f4b2e71b0eb71",
				"img_logo_url": "00839c42bc838b46bee465c13f307f63cf2339f6"
			},
			{
				"appid": 340200,
				"name": "Bloop",
				"playtime_forever": 36,
				"img_icon_url": "8672944966d57a030660099122a4648b598b7e3c",
				"img_logo_url": "970be632b90e7db2022388eaeb48573c9ea64770"
			}
		]
		
	}
}
К примеру:
Код:
"game_count": 44,
равнялось $array[0]
Код:
				"appid": 240,
				"name": "Counter-Strike: Source",
				"playtime_2weeks": 25,
				"playtime_forever": 23091,
				"img_icon_url": "9052fa60c496a1c03383b27687ec50f4bf0f0e10",
				"img_logo_url": "ee97d0dbf3e5d5d59e69dc20b98ed9dc8cad5283",
				"has_community_visible_stats": true
appid равнялся $array[$i][1] , а name равнялся $array[$i][2]
Использовал различный библиотеки для того чтобы раздекодить json но ни одна не хочет его обрабатывать.
 

alex33

Скриптер
Сообщения
1,457
Репутация
186
Код:
Global $oData, $sCode
$sCode = FileRead(@ScriptDir & "\1.json")
$oData = _jsonDecode($sCode)
If @error Then Exit @error
For $oItem In $oData.response.games
	ConsoleWrite($oItem.name & @CRLF)
Next

Func _jsonDecode($sStr)
	Local $oSC = ObjCreate("ScriptControl")
	If Not IsObj($oSC) Then Return SetError(1, 0, 0)
	$oSC.Language = "JavaScript"
	$sStr = StringRegExpReplace($sStr, '[\r\n]', ' ')
	$sStr = StringReplace($sStr, "'", "\'")
	$oResult = $oSC.Eval("eval('(" & $sStr & ")');")
	If Not IsObj($oResult) Then Return SetError(2, 0, 0)
	Return $oResult
EndFunc   ;==>_jsonDecode

Результат после выполнения:
Код:
--> Press Ctrl+Alt+F5 to Restart or Ctrl+Break to Stop
Counter-Strike: Source
Day of Defeat: Source
Half-Life 2: Deathmatch
Half-Life 2: Lost Coast
Garry's Mod
Commandos 3: Destination Berlin
Avencast
Metro 2033
BEEP
Rock of Ages
Defy Gravity
Counter-Strike: Global Offensive
Super Monday Night Combat
Loadout
Dungeonland
War Thunder
HAWKEN
War of the Roses
Free to Play
Happy Wars
Grimm
Cubetractor
Teleglitch: Die More Edition
PAYDAY 2
Sins of a Dark Age
Enclave
East India Company Gold
KnightShift
Quake Live
ACE - Arena: Cyber Evolution
The Hat Man: Shadow Ward
Racer 8
Realms of the Haunting
The Culling Of The Cows
Robocraft
Unturned
Borealis
Heroes & Generals
Dead Island: Epidemic
Nosgoth
Dota 2 Test
PAYDAY: The Heist
Grimoire: Manastorm
Bloop
>Exit code: 0    Time: 0.4599
 

madmasles

Модератор
Глобальный модератор
Сообщения
7,790
Репутация
2,322
Kasper,
Предупреждение За нарушение общих правил (пункт В.2):
Старайтесь избегать “Over quoting” (преувеличенное цитирование) - цитируйте только необходимую часть сообщения, которая наилучшим образом подчеркнёт суть цитируемого.


С уважением, ваш Глобальный модератор.
 
Автор
K

Kasper

Знающий
Сообщения
70
Репутация
8
alex33, У меня ваш код устанавливает ошибку 1, 0, 0 и скрипт завершает свою работу.
Как мне ее исправить?
 

alex33

Скриптер
Сообщения
1,457
Репутация
186
Kasper сказал(а):
alex33, У меня ваш код устанавливает ошибку 1, 0, 0 и скрипт завершает свою работу.
Как мне ее исправить?
Поставить нормальную версию AutoIt, например 3.3.12.0.
 

scrdj

Новичок
Сообщения
2
Репутация
0
Здравствуйте. Схожая проблема с автором темы.
Мой JSON
{
"request": {
"Target": "Target",
"Format": "jsonp",
"Service": "Service",
"Version": "2",
"Key1": "key1",
"Method": "method",
"Key2": "key2",
"fields": [
"field1",
"field2"
],
"callback": "callback",
"Key3": "key3",
"Key4": "key4"
},
"response": {
"status": 1,
"httpStatus": 200,
"data": {
"num1": {
"Res": {
"field1": "name",
"field2": "des"
}
},
"num2": {
"Res": {
"field1": "name",
"field2": "des"
}
}
},
"errors": [],
"errorMessage": null
}
}

требуется получить массив. Пробовал использовать функцию alex33
Но я не понимаю что делать с объектом $oData что бы получить из него массив например всех field1
 

alex33

Скриптер
Сообщения
1,457
Репутация
186
Код:
ConsoleWrite( $oData.response.data.num1.Res.field1 & @TAB & $oData.response.data.num2.Res.field1 & @CRLF)



Добавлено:
Сообщение автоматически объединено:

JavaScript:
var oData={
  "request": {
    "Target": "Target",
    "Format": "jsonp",
    "Service": "Service",
    "Version": "2",
    "Key1": "key1",
    "Method": "method",
    "Key2": "key2",
    "fields": [
      "field1",
      "field2"
    ],
    "callback": "callback",
    "Key3": "key3",
    "Key4": "key4"
  },
  "response": {
    "status": 1,
    "httpStatus": 200,
    "data": {
      "num1": {
        "Res": {
          "field1": "name",
          "field2": "des"
        }
      },
      "num2": {
        "Res": {
          "field1": "name",
          "field2": "des"
        }
      }
    },
    "errors": [],
    "errorMessage": null
  }
}


// alert(oData.response.data.num1.Res.field1 +"\t"+ oData.response.data.num2.Res.field1);

for (var oItem in oData.response.data) {
	alert( oItem );
// num1
// num2
}
А вот дальше что-то не перебирается. Если внутри этого цикла ещё один вложенный сделать, то он будет выдавать какие-то цифры, 1, 2, 0... А на остальные поля - undefind. Странно...
 

C2H5OH

AutoIT Гуру
Сообщения
1,473
Репутация
333
Код:
#include <File.au3>
#include <Array.au3>

$sText = FileRead("json_k.txt")

$aFields = _ExtractJsonField( $sText, "field1")

_ArrayDisplay($aFields)

Func _ExtractJsonField( $JSON, $FieldName)

	$pattern = '(?m)'& $FieldName & '"\:\s*?(\d+?|".*?")\,{0,1}$'
	$aRes = StringRegExp($JSON, $pattern, 3)

	Return $aRes

EndFunc   ;==> _ExtractJsonField
 
Верх