import sys
import json
notFirst = False
print("[") # start json array
for data in sys.stdin:
(name,text) = data.strip().split("|") # split line
obj = dict(name=name, text=text) # to dict()
if notFirst: # avoid first comma for object
print(",", end='')
print(json.dumps(obj, ensure_ascii=False)) # dump json object
notFirst = True
print("]") # end json array
cat mydatafile.txt | python3 txttojson.py > mydatafile.json
[
{"text": "Не ела и не курила", "name": "Лошадь"}
,{"text": "Бездушна как всегда", "name": "Розетка"}
,{"text": "Тоже человек", "name": "Насос"}
]
import json
myjson = """
{
"accounts": [
{
"alias": "string",
"balance": {
"amount": 0,
"currency": 0
},
"bankAlias": "string",
"currency": 0,
"defaultAccount": true,
"fsAlias": "string",
"hasBalance": true,
"title": "string",
"type": {
"id": "string",
"title": "string"
}
}
]
}
"""
data = json.loads(myjson)
print data["accounts"][0]["balance"]["amount"]
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("4");
list.add("1");
int summ = list.stream()
.map(s -> Integer.parseInt(s)) // перевод из String в int
.reduce( (s1,s2) -> s1 + s2 ) // сумма
.orElse(0); // значение по умолчанию
System.out.println(summ);
public class Reduce {
public static class Offer {
int price;
public Offer (int price) {
this.price = price;
}
public int getPrice() {
return price;
}
}
public static void main(String args[]) {
List< Offer > list = new ArrayList<>();
list.add(new Offer(1));
list.add(new Offer(2));
list.add(new Offer(4));
list.add(new Offer(1));
int summ = list.stream()
.map(Offer::getPrice) // взять прайс
.reduce( (s1,s2) -> s1 + s2 ) // сумма
.orElse(0); // значение по умолчанию
System.out.println(summ);
}
}