How can I view the raw data of an Ethereum block? - ethereum

I'm trying to view the raw data byte by byte of an block in the Ethereum blockchain.
I want to see how its being actually added in the chain.
the closet thing I found is web3.eth.getBlock(1212121,true) with an ethconsole. The result contains some elements I'm not sure if they are in the block like "size" or "totalDifficulty". this result of the command is:
> web3.eth.getBlock(1212121,true)
{ difficulty: '20632001790827',
extraData: '0xd783010305844765746887676f312e352e31856c696e7578',
gasLimit: 4712388,
gasUsed: 409654,
hash: '0xe58280354d08c9fef9c4cfeff54e3c9257db6508c5594a14fcaab55c33aff7ae',
logsBloom: '0x40000000000000000002000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000008000000000000000800000108000100000000000000401000020000000000000000800a02000000000000000000000090000000000000000000000020100000000000000000000000000000000002800000000000000000000000000000020000000000000000000000000408000000000000010000000002000000000000000000000000000000000000000000000000008020000000000400000000000000000000000000400000000000000000000001000000',
miner: '0x2a65Aca4D5fC5B5C859090a6c34d164135398226',
mixHash: '0xe9c763a3b4c2fd3c25ce2909b2b4be610d34ed4c6bfb25ae30929cf9b4fbf37f',
nonce: '0xa62df06cbd58960e',
number: 1212121,
parentHash: '0x33dc0cd459f5c9befc3866727494b1d4c7af1aefc7361b9343aecc7e4ef75c49',
receiptsRoot: '0xcd45d340d3e4f8cb92724b434f9e74d5972fca7240fffa3e6d2d4a1a9b84874c',
sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
size: 2866,
stateRoot: '0xc929d0dbf6abaf1178dd24434ca66c95847540eb49a4c86689f64b63513e1ecd',
timestamp: 1458873705,
totalDifficulty: '10587466690743571712',
transactions:
[ { blockHash: '0xe58280354d08c9fef9c4cfeff54e3c9257db6508c5594a14fcaab55c33aff7ae',
blockNumber: 1212121,
from: '0x90384D665f5687be20FA3EfC029939d249f0570c',
gas: 250000,
gasPrice: '20000000000',
hash: '0x79d6d88c9662437dac7064549fbf77d35d1da1c2246b5ec729686f3f56add401',
input: '0x3ed486790000000000000000000000002e6480c43704100a7f60f6006d94aff5a5801f490000000000000000000000000000000000000000000000000000000056f4a56100000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000835958229c6adfd769fc836ceaedd41e15d6290300000000000000000000000000000000000000000000000000000000021fd2c00000000000000000000000000000000000000000000000000000000000000002affb2869dcbe152a484a945026a2de025419971f335d636db4aa9b2070c47d5d0474f35ba4a3bbf111b7cbe746094fb248ef80cbb1790b302fcf972d8bb1a8bb0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000001c',
nonce: 2914,
to: '0xaD62F56a03334B647E55dbdB5B8642C24605a801',
transactionIndex: 0,
value: '1',
v: '0x1b',
r: '0x8ca42b848ed12cc18f20c152ecf597f62576c600d663a09dd69c0ee8d50829fd',
s: '0x1f8ee0543d8b7b2a5484dfb0b0d5f8c8e676b2a49cf3a1e53b2d85a3c64c1af3' },
{ blockHash: '0xe58280354d08c9fef9c4cfeff54e3c9257db6508c5594a14fcaab55c33aff7ae',
blockNumber: 1212121,
from: '0x90384D665f5687be20FA3EfC029939d249f0570c',
gas: 250000,
gasPrice: '20000000000',
hash: '0x52e6fb23ebd870deace5a8ded5809a8184bc277a07874f821642d38639da1a66',
input: '0x3ed486790000000000000000000000002e6480c43704100a7f60f6006d94aff5a5801f490000000000000000000000000000000000000000000000000000000056f4a56200000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000835958229c6adfd769fc836ceaedd41e15d62903000000000000000000000000000000000000000000000000000000000069be50000000000000000000000000000000000000000000000000000000000000000208a26863e7ed834c95d44ae453428ae5dab27344c93ebf81ac7e8db961f83ad12191c6e6d73b2f1036e62310f65f0d7d54fc887b24432d95f9a051d3bb95c3660000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000001b',
nonce: 2915,
to: '0xaD62F56a03334B647E55dbdB5B8642C24605a801',
transactionIndex: 1,
value: '1',
v: '0x1c',
r: '0xa55b8f2203be4d79653a50ca729b126499ef67c20ee3e1fbbd82507f3527f83a',
s: '0xd853f6e2d4914cee9cbea8d4eb5d874a2a7e6b7a7223b451c504a2a30f75ad3' },
{ blockHash: '0xe58280354d08c9fef9c4cfeff54e3c9257db6508c5594a14fcaab55c33aff7ae',
blockNumber: 1212121,
from: '0x90384D665f5687be20FA3EfC029939d249f0570c',
gas: 250000,
gasPrice: '20000000000',
hash: '0x4b64e1af8df53d6479dcd9c7ef36a8128cbdd85be07fbe0848bb5c8f18f88868',
input: '0x3ed486790000000000000000000000002e6480c43704100a7f60f6006d94aff5a5801f490000000000000000000000000000000000000000000000000000000056f4a56300000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000012000000000000000000000000060be9422457ce5d68701ac893d9ec9db639792f200000000000000000000000000000000000000000000000000000000046cf7100000000000000000000000000000000000000000000000000000000000000002758066c4fcb14ad292b3d714e0c791c510fb16e5a5d97d469f4dca7a718c7de31c4979e591ddaf14861bf66380fc799d4b2f2ea7615bd8a34d8eecead15aa77e0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000001c',
nonce: 2916,
to: '0xaD62F56a03334B647E55dbdB5B8642C24605a801',
transactionIndex: 2,
value: '1',
v: '0x1c',
r: '0x8b41131ed19fc8fa5888c59a5f15084b3a9f5963d6d62b69ebed8791cca2a3fe',
s: '0x251070a69ad098def1049fd69818d337239ddd705379b2b7b8ddc94fed4225bc' },
{ blockHash: '0xe58280354d08c9fef9c4cfeff54e3c9257db6508c5594a14fcaab55c33aff7ae',
blockNumber: 1212121,
from: '0x90384D665f5687be20FA3EfC029939d249f0570c',
gas: 250000,
gasPrice: '20000000000',
hash: '0x86ee9f55ec67268a5507a64c9591299c5b5b173e408728e45d8da851703f8b5d',
input: '0x3ed486790000000000000000000000002e6480c43704100a7f60f6006d94aff5a5801f490000000000000000000000000000000000000000000000000000000056f4a56500000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001200000000000000000000000008782b18597724d71d337e408e25c1a00c49a2a1b00000000000000000000000000000000000000000000000000000000132d83f00000000000000000000000000000000000000000000000000000000000000002f7949f93b8fb7983f4c786292c1210d4aef088fe5ddc59a11d6a12795450b2747dbd6dedbee599369e64ec95d364b0c7f80ed75816078d02f0dbab40fa3b944c0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000001c',
nonce: 2917,
to: '0xaD62F56a03334B647E55dbdB5B8642C24605a801',
transactionIndex: 3,
value: '1',
v: '0x1c',
r: '0xf055c79b4bed2d9ea6518fba84e55efeb43b93f142fa639c753872acbd3c29fb',
s: '0x2e97c34681b2d7389a53d8c605be91ee1d0904e1582e4f58abec82abbc97a088' },
{ blockHash: '0xe58280354d08c9fef9c4cfeff54e3c9257db6508c5594a14fcaab55c33aff7ae',
blockNumber: 1212121,
from: '0x90384D665f5687be20FA3EfC029939d249f0570c',
gas: 250000,
gasPrice: '20000000000',
hash: '0xbf4b4bbaeae0fffecf4a693e2f4b7d13790c4d120709e21a3b5d19b7f7be2e51',
input: '0x3ed486790000000000000000000000002e6480c43704100a7f60f6006d94aff5a5801f490000000000000000000000000000000000000000000000000000000056f4a56600000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001200000000000000000000000006f180ebbe72e7033b47d6937c11a64947b2e06ec0000000000000000000000000000000000000000000000000000000000e297d00000000000000000000000000000000000000000000000000000000000000002f3d50c6e9894955652f3a21d3cc531636fd7ad574fc9152298c91af3fefdbb5833691995bb8c2b5de36ac6df4b37e578c5847a176e5e81c6ec65f5da8091d28d0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000001b',
nonce: 2918,
to: '0xaD62F56a03334B647E55dbdB5B8642C24605a801',
transactionIndex: 4,
value: '1',
v: '0x1b',
r: '0x14c49a89bfd848640c8a942158322f402a2d7839e55e9bf1a71c99c11735d5a9',
s: '0x31dbb910f8d18c8ba3ab59c4f28d8ca096a25d71ca1ec2f0c1228259b6cce10' } ],
transactionsRoot: '0x9a66f376a40ecc62c3ea600bf2371f1929d5cc7d66fbf48e8ac9a64d61fbc295',
uncles: [] }

See the answer from #WesternZealot in reddit:
https://ethereum.github.io/yellowpaper/paper.pdf
See Section 4.3 all the way to the end of 4.3.3. This will give you
the understanding of how the data is serialized in the block.
Then to inspect an actual block, you should run an Ethereum client
(such as Geth) in full node mode until at least one block has been
downloaded, then find where the block is located on your computer and
inspect its contents.

Related

jsondecode loading issue in dart

I try to run below code in flutter App but it loads half JSON data, same API when running for a web app it works fine.
Any idea how to solve this issue?
Example
Future <List> test() async{
final response = await http.post(
"https://xyz/allData",
body: jsonEncode({
"email": "email"
}),
headers: {
HttpHeaders.contentTypeHeader: ContentType.json.toString(),
HttpHeaders.authorizationHeader: token
});
final data= json.decode(response.body);
OR
Map<String, dynamic> data =json.decode(response.body);
Error
half JSON fetch by above code
below content I recieved :
{success: true, msg: Loaded, content:
{total: 3, on: 0, off: 3,
on: {},
off: {
11: {id: 11, last_data: N/A,value: {N/A: {name: N/A, value: N/A}},
name: 01, type: temp, upper: 20, lower: 10, email: [], phn_list: [], id: 01,status: 0, freq: 10, description: 01, status_notf: 0, lat_map: 0, lng_map: 0, html: html},
12: {id: 12, last_data: N/A, value: {N/A: {name: N/A, value: N/A}},
name: 02, type: temp, upper: 20, lower: 10, email: [], phn_list: [], id: 02, status: 0,freq: 10, description: 02, status_notf: 0, lat_map: 0, lng_map

How to parse JSON information then select information and assign it to a variable in Swift

I am working on an app that reads a public JSON for Bike availability and many other data.
Structs located here:
struct Stations: Codable {
let company: [String]
let href: String
let id: String
let location: LocationJson
let name: String
let stations: [Station]
}
struct Station: Codable {
let empty_slots: Int
let extra: Extra
let free_bikes: Int
let id: String
let latitude: Double
let longitude: Double
let name: String
let timestamp: String
public init?(empty_slots:Int, free_bikes:Int, id:String) {
self.empty_slots = empty_slots
self.id = id
self.free_bikes = free_bikes
return nil
}
}
struct ResponseJSON: Codable {
let network: Stations
}
struct LocationJson: Codable {
let city: String
let country: String
let latitude: Double
let longitude: Double
}
struct Extra: Codable {
let slots: Int
let status: String
let uid: String
init(slots:Int, status: String, uid: String) {
self.slots = slots
self.status = status
self.uid = uid
}
}
Naturally every single one of these stations has massive amount of data. I am able to read the JSON File, however I am not able to select and use the information I am receiving. I am including the function that I wrote below:
func getBikeData(){
guard let url = URL(string: "https://api.citybik.es//v2/networks/baksi-bisim")
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let dataResponse = data, error == nil else {
print(error?.localizedDescription ?? "Response Error")
return
}
do {
//here dataResponse received from a network request
let jsonResponse = try JSONSerialization.jsonObject(with:
dataResponse, options: [])
print(jsonResponse) //Response result
do {
//here dataResponse received from a network request
let decoder = JSONDecoder()
//Decode JSON Response Data
let model = try decoder.decode(
ResponseJSON.self, from: dataResponse
)
print(model)//Output - 1221
}
catch let parsingError {
print("Error", parsingError)
}
} catch let parsingError {
print("Error", parsingError)
}
}
task.resume()
}
I really need help on how to find and assign Station data from the JSONResponse and allocate it to the needed variables to show a label and more useful information. How can I implement this function to perform such task?
Also Output included below:
ResponseJSON(network: MapKitTest.moreDetail.Stations(company: ["Baksi Bike Sharing System"], href: "/v2/networks/baksi-bisim", id: "baksi-bisim", location: MapKitTest.moreDetail.LocationJson(city: "Izmir", country: "TR", latitude: 38.4134047464, longitude: 27.0325753524), name: "Baksi", stations: [MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 40, status: "Active", uid: "1101"), free_bikes: 40, id: "81a2def326cc97187e41666320e38dbe", latitude: 38.4134047464, longitude: 27.0325753524, name: "İnciraltı Rekreasyon Alanı", timestamp: "2020-05-05T05:30:25.331000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 40, status: "Active", uid: "110101"), free_bikes: 40, id: "b8568b58ad34aeee55764593d36bbb71", latitude: 38.411389, longitude: 27.013472, name: "Sahilevleri 1", timestamp: "2020-05-05T05:30:25.334000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 20, status: "Active", uid: "110102"), free_bikes: 20, id: "7d33a4fccc471ee42853ed0ad74d214a", latitude: 38.406137, longitude: 26.996162, name: "Sahilevleri 2", timestamp: "2020-05-05T05:30:25.335000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 40, status: "Active", uid: "1102"), free_bikes: 40, id: "89c5c6d0e1c035629d129ed1f4acf5bc", latitude: 38.4066894063, longitude: 27.0618320135, name: "İnciraltı Kent Ormanı", timestamp: "2020-05-05T05:30:25.336000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 30, status: "Active", uid: "1103"), free_bikes: 30, id: "609f9c4238891bc09936cd7a098af35c", latitude: 38.4047512791, longitude: 27.0701936867, name: "Üçkuyular İskelesi", timestamp: "2020-05-05T05:30:25.337000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 20, status: "Active", uid: "1104"), free_bikes: 20, id: "aa2935c1ec78fc31e5c1c567efbd0594", latitude: 38.4006949388, longitude: 27.0781865266, name: "A.A Saygun", timestamp: "2020-05-05T05:30:25.339000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 30, status: "Active", uid: "1105"), free_bikes: 30, id: "183d94471151914258dea18731ff7da3", latitude: 38.3992000613, longitude: 27.0837509082, name: "Göztepe Köprü", timestamp: "2020-05-05T05:30:25.340000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 15, status: "Active", uid: "1106"), free_bikes: 15, id: "1e7f533e09a98c0b7b84a370aa4ecaa6", latitude: 38.4021639086, longitude: 27.0930261205, name: "Susuzdede", timestamp: "2020-05-05T05:30:25.341000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 15, status: "Active", uid: "1107"), free_bikes: 15, id: "44d388d823270744179416a6ac073663", latitude: 38.4062155783, longitude: 27.0987910553, name: "Köprü", timestamp: "2020-05-05T05:30:25.342000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 15, status: "Active", uid: "1108"), free_bikes: 15, id: "9cda37d23a942db3cde2af205a399fd3", latitude: 38.4090090197, longitude: 27.1089924616, name: "Karantina", timestamp: "2020-05-05T05:30:25.344000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 15, status: "Active", uid: "1109"), free_bikes: 15, id: "61b2982a8e496e2909e55e4f382fd11e", latitude: 38.4116478229, longitude: 27.1201230494, name: "Karataş", timestamp: "2020-05-05T05:30:25.345000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 38, status: "Active", uid: "1110"), free_bikes: 38, id: "b1eb2f80735902baf93de87d3474a351", latitude: 38.4187847458, longitude: 27.1264456215, name: "Konak İskele", timestamp: "2020-05-05T05:30:25.346000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 20, status: "Active", uid: "111001"), free_bikes: 20, id: "c4cd8015779e1633d8dd8e8792abb1c3", latitude: 38.416539, longitude: 27.127547, name: "Konak Metro ", timestamp: "2020-05-05T05:30:25.347000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 20, status: "Active", uid: "1111"), free_bikes: 20, id: "11d0c49d801d6f4bffd986c8e759e3c4", latitude: 38.4275173482, longitude: 27.1325844103, name: "Pasaport İskele", timestamp: "2020-05-05T05:30:25.348000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 20, status: "Active", uid: "1112"), free_bikes: 20, id: "147af47ab155d7630fa0d6318794c886", latitude: 38.4303211039, longitude: 27.1342384751, name: "Vasıf Çınar", timestamp: "2020-05-05T05:30:25.349000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 12, status: "Active", uid: "1113"), free_bikes: 12, id: "99ba54bf2aa81d0fad7fea5a3e1dc7e7", latitude: 38.4340526994, longitude: 27.1380971058, name: "Ali Çetinkaya Bulvarı", timestamp: "2020-05-05T05:30:25.351000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 21, status: "Active", uid: "1114"), free_bikes: 21, id: "a282e085c85c42f5dc6d37cd2a41e239", latitude: 38.4384639223, longitude: 27.1414140426, name: "Alsancak İskele", timestamp: "2020-05-05T05:30:25.352000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 12, status: "Active", uid: "1115"), free_bikes: 12, id: "d3b95b4926f1c641cc3e726555cdea6c", latitude: 38.4421949873, longitude: 27.1432792183, name: "Liman", timestamp: "2020-05-05T05:30:25.353000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 15, status: "Active", uid: "1116"), free_bikes: 15, id: "8ea919ee44cc8a8b9c21c78241b6f90b", latitude: 38.439948857, longitude: 27.1478470394, name: "Alsancak Garı", timestamp: "2020-05-05T05:30:25.354000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 22, status: "Active", uid: "1117"), free_bikes: 22, id: "678444256ec651a9ec27796473f4dbc7", latitude: 38.4451831829, longitude: 27.1707578568, name: "Meles Rekreasyon Alanı", timestamp: "2020-05-05T05:30:25.359000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 20, status: "Active", uid: "111701"), free_bikes: 20, id: "0dc0e46284e114af08922a0de6b2fe9c", latitude: 38.468382, longitude: 27.211666, name: "Buz Pisti 1", timestamp: "2020-05-05T05:30:25.360000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 15, status: "Active", uid: "1118"), free_bikes: 15, id: "36e009dd4f4a2e174c9ad794cccb0304", latitude: 38.4635758915, longitude: 27.1627288532, name: "Bayraklı İskele", timestamp: "2020-05-05T05:30:25.362000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 12, status: "Active", uid: "1119"), free_bikes: 12, id: "73df94d74b8e9010f7326b1a3e40eb75", latitude: 38.4667637722, longitude: 27.158891294, name: "Bayraklı Nikah Salonu", timestamp: "2020-05-05T05:30:25.363000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 12, status: "Active", uid: "1120"), free_bikes: 12, id: "570ff88190f008480dc00eeab22b7581", latitude: 38.4671289884, longitude: 27.1495805948, name: "İzban Turan İstasyonu", timestamp: "2020-05-05T05:30:25.365000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 3, status: "Active", uid: "1122"), free_bikes: 3, id: "36da158bb8190f739ba73da40eab4b95", latitude: 38.4552765126, longitude: 27.120318845, name: "Karşıyaka iskele", timestamp: "2020-05-05T05:30:25.368000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 12, status: "Active", uid: "1123"), free_bikes: 12, id: "3aa1d333fb31ec7ba88526aee7f899f9", latitude: 38.4494793514, longitude: 27.1105867977, name: "Karşıyaka Evlendirme Dairesi", timestamp: "2020-05-05T05:30:25.369000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 12, status: "Active", uid: "1124"), free_bikes: 12, id: "11d48f2f7823aa3f21c8f36b78c7fe20", latitude: 38.4511789658, longitude: 27.1041118947, name: "Yunuslar", timestamp: "2020-05-05T05:30:25.370000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 12, status: "Active", uid: "1125"), free_bikes: 12, id: "ad4e0f95996a8a20fd68cbafe8db408b", latitude: 38.45471, longitude: 27.096761, name: "Bostanlı iskele ", timestamp: "2020-05-05T05:30:25.371000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 24, status: "Active", uid: "1126"), free_bikes: 24, id: "d6353dd7ab8c3ca0f4e4d002c437e382", latitude: 38.456065695, longitude: 27.0926334293, name: "Bostanlı Odağı", timestamp: "2020-05-05T05:30:25.372000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 40, status: "Active", uid: "1127"), free_bikes: 40, id: "3c6f736309838b74754c0625ca2ce3d7", latitude: 38.4622502557, longitude: 27.0849728096, name: "Bostanlı Spor Tesisleri", timestamp: "2020-05-05T05:30:25.373000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 12, status: "Active", uid: "1128"), free_bikes: 12, id: "914e4271e4747ad8a12539cd320a3f5f", latitude: 38.465435, longitude: 27.0817107, name: "Mavişehir Balıkçı Barınağı", timestamp: "2020-05-05T05:30:25.374000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 8, status: "Active", uid: "1129"), free_bikes: 8, id: "eaf112af663ad0ad37c3863fa703594f", latitude: 38.4674834, longitude: 27.0793412, name: "Mavişehir", timestamp: "2020-05-05T05:30:25.376000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 30, status: "Active", uid: "1131"), free_bikes: 30, id: "415ca0c2cf9cf73b30db66b7a25990ef", latitude: 38.533134, longitude: 26.898589, name: "Kuş Cenneti", timestamp: "2020-05-05T05:30:25.378000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 24, status: "Active", uid: "1132"), free_bikes: 24, id: "fb7ea8f96edb19733d2807294a470662", latitude: 38.487952, longitude: 26.958984, name: "Doğal Yaşam Parkı", timestamp: "2020-05-05T05:30:25.379000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 12, status: "Active", uid: "1121"), free_bikes: 12, id: "62686b23d8531f66f204487ec2500c6a", latitude: 38.460954, longitude: 27.127738, name: "Alaybey Tersane Cafe", timestamp: "2020-05-05T05:30:25.366000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 20, status: "Active", uid: "111702"), free_bikes: 20, id: "5abb28cac806012786d90efd4f5b31d0", latitude: 38.46576, longitude: 27.207689, name: "Buz Pisti 2", timestamp: "2020-05-05T05:30:25.361000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 15, status: "Active", uid: "111605"), free_bikes: 15, id: "55ca65e3f8c19fe8bc06cb1c6759a21f", latitude: 38.424372, longitude: 27.143219, name: "Fuar Basmane", timestamp: "2020-05-05T05:30:25.358000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 15, status: "Active", uid: "111603"), free_bikes: 15, id: "ac286589d8a8178f2c52d2f15889d338", latitude: 38.42975, longitude: 27.142266, name: "Fuar Lozan", timestamp: "2020-05-05T05:30:25.355000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 15, status: "Active", uid: "111604"), free_bikes: 15, id: "77c9cfeeefb97b24adbf92992dfc6bc8", latitude: 38.427879, longitude: 27.14175, name: "Fuar Montrö", timestamp: "2020-05-05T05:30:25.356000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 12, status: "Active", uid: "1130"), free_bikes: 12, id: "ac93bdd032b07712b7d5ba8b5437a972", latitude: 38.4749864, longitude: 27.0747371, name: "Mavi Bahçe", timestamp: "2020-05-05T05:30:25.377000Z"), MapKitTest.moreDetail.Station(empty_slots: 0, extra: MapKitTest.moreDetail.Extra(slots: 0, status: "Active", uid: "1300"), free_bikes: 0, id: "c4311294e7ce99c966cc9ae4f0259fd1", latitude: 38.4187094, longitude: 27.1250995, name: "Bisim Üye Noktası", timestamp: "2020-05-05T05:30:25.382000Z")]))
Here is how you can proceed.
Let's assume you want to fetch stationName from stations array,
let stationName = "A.A Saygun"
Now, you can use first(_:) on stations array to get the matching Station instance,
let model = try JSONDecoder().decode(ResponseJSON.self, from: data)
if let station = model.network.stations.first(where: { $0.name == stationName }) {
//get the properties of station here as per requirement
let emptySlots = station.emptySlots
let freeBikes = station.freeBikes
let status = station.extra.status
}
Alamofire.request("https://api.citybik.es//v2/networks/baksi-bisim").responseJSON { json in
/// do what you want with your JSON e.g
let stationName = json["network"][0]["name"].string ?? ""
}
OR
let json = JSON(data: dataFromNetworking)
if let stationName = json["network"][0]["name"].string {
// Now you got your value
}

Spring + React fetch data

i have a problem with fetch data from api exposed by spring.
I want to fetch data by:
componentDidMount() {
this.setState({isLoading: true});
fetch('http://localhost:8080/api/tasks/')
.then(response => { return response.json() })
.then(results => this.setState({
tasks: results,
isLoading: false
}));
The spring api which return data:
#CrossOrigin(origins = "http://localhost:3000")
#GetMapping
public List<Task> findTasksByUserId() {
return taskService.findTasksBelongToUser(idProvider.getCurrentUserId());
}
Returned JSON:
[
{
id: 1,
version: 0,
name: "Task1",
description: "description1",
priority: 1,
finished: true,
category: {
id: 1,
version: 0,
name: "Uncategorized",
user: {
id: 1,
version: 0,
login: "admin",
email: "admin#todo.pl",
enabled: true
}
},
user: {
id: 1,
version: 0,
login: "admin",
email: "admin#todo.pl",
enabled: true
},
position: 0
},
{
id: 2,
version: 0,
name: "Task2",
description: "description2",
priority: 4,
finished: true,
category: {
id: 1,
version: 0,
name: "Uncategorized",
user: {
id: 1,
version: 0,
login: "admin",
email: "admin#todo.pl",
enabled: true
}
},
user: {
id: 1,
version: 0,
login: "admin",
email: "admin#todo.pl",
enabled: true
},
position: 1
}]
And i got a error like this:
Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
I dont know what is wrong but maybe format of json is not correct, because it's start with the sign '['.
I am a beginner in React so please about some hint and help.
regards
fetch() returns a promise which gets resolved into an HTTP response of course, not the actual JSON. To extract the JSON body content from the response, we use the json() method.
Try adding two headers Content-Type and Accept to be equal to application/json.
fetch('http://localhost:8080/api/tasks/', {
headers : {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
})
.then(response => { return response.json() })
.then(results => this.setState({
tasks: results,
isLoading: false
}));

Node.js: parsing JSON object from an Elasticsearch query

I have an elasticsearch database that I access through a node.js client. I can parse the JSON from elasticsearch to print out the hitsArray which looks like below:
[ { _index: 'parties',
_type: 'suppliers',
_id: 'AV0uELknL82XeGsCOZ-i',
_score: 1,
_source: { name: 'Jabil', address: [Object], rating: 4.2 } },
{ _index: 'parties',
_type: 'suppliers',
_id: 'AV0t_yC3L82XeGsCOZ-f',
_score: 1,
_source: { name: 'Apple', address: [Object], rating: 4.9 } },
{ _index: 'parties',
_type: 'suppliers',
_id: 'AV0t_glkL82XeGsCOZ-d',
_score: 1,
_source: { name: 'Flextronics', address: [Object], rating: 4.5 } },
{ _index: 'parties',
_type: 'suppliers',
_id: 'AV0t_ox7L82XeGsCOZ-e',
_score: 1,
_source: { name: 'FlashMob', address: [Object], rating: 3.5 } } ]
Now, I want to parse the _source field and print out the name field
client.search(searchParams).then(function (resp) {
return Promise.all(resp.hits.hits)
}).then(function(hitsArray){
hitsArray.forEach(function(value){
JSONObject first = hitsArray.getJSONObject(value);
JSONObject source = first.getJSONObject("_source");
String name = source.getString("name");
console.log(name);
});
});
But I am getting error
JSONObject first = hitsArray.getJSONObject(value);
^^^^^
SyntaxError: Unexpected identifier
What am I missing? any suggestion?
JavaScript has no explicit type definitions like that. It looks like those 3 lines may be Java code and not JavaScript code.
As far as getting the actual JavaScript value goes, it appears to already be an object. Just access it with value._source (and value._source.name).

Process JSON using jQuery

I have the following JSON-encoded data being returned and need to process it with jQuery.
How can I access different depots and vehicle data inside this list using jQuery.getJSON()'s callback function?
$.getJSON('url', function(data) {
// ...?
});
The JSON-encoded data:
// top-level result is a list of dictionaries
[
// return dictionary for each depot
{
depot: {
_id: 'D3',
intersection: {
first: 'Bay',
second: 'King'
},
address: {
number: '100',
street: 'King street West',
city: 'Toronto',
province: 'ON',
postal_code: 'M5X 1B8'
},
},
// return dictionary for each car in that depot
vehicle: [{
_id: 'V4',
_depot_id: 'D3',
model: 'Ford F150',
price: '80',
km_per_litre: '15',
cargo_cu_m: 'YES',
category: 'Truck',
image: 'www.coolcarz.com'
}, {
_id: 'V24',
_depot_id: 'D3',
model: 'Toyota Camry Hybrid',
price: '90',
km_per_litre: '25',
cargo_cu_m: 'YES',
category: 'Hybrid car',
image: 'www.coolcarz.com'
}
]
},
{
depot: {
_id: 'D9',
intersection: {
first: 'Bay',
second: 'Front'
},
address: {
number: '161',
street: 'Bay',
city: 'Toronto',
province: 'ON',
postal_code: 'M5J 2S1'
},
},
// return dictionary for each car in that depot
vehicle: [{
_id: 'V11',
_depot_id: 'D9',
model: 'Ford Crown Victoria',
price: '45',
km_per_litre: '13',
cargo_cu_m: 'YES',
category: 'Standard car',
image: 'www.coolcarz.com'
},
]
},
]
$.getJSON('url', function(data) {
alert(data.length); // 2
alert(data[0].depot.intersection.second); // "King"
alert(data[0].vehicle[1].category); // "Hybrid car"
alert(data[1].depot.address.city); // "Toronto"
});