从文件上传中解析JSON数据

时间:2018-05-01 19:30:15

标签: php json laravel laravel-5 laravel-5.1

我有一个选择json文件的输入

在我的控制器中,我做了

dd(Input::all());

我得到了

enter image description here

我的目标是解析我得到的JSON文件并循环遍历它们。

我试过

$string = file_get_contents(Input::get('fileinput'));
$json = json_decode($string, true);

问题

如何继续进行调试?

我现在对任何建议持开放态度。

任何提示/建议/帮助都将非常感谢!

3 个答案:

答案 0 :(得分:2)

Input::get用于从请求中检索输入项($ _REQUEST), 您应该使用Input::file代替Illuminate\Http\UploadedFile,用于从请求中检索文件,并返回<?php $file = Input::file('fileinput'); if($file === null) { throw new Exception('File was not sent !'); } if($file->isReadable()) { $file->open('r'); $contents = $file->fread($file->getSize()); $json = json_decode($contents, true); } else { throw new Exception('File is not readable'); } 实例。

示例:

import time
import math
import random
dict_of_lists={}
def addlist():
  while True:
    try:
      listname=str(raw_input("=>Name of the list:\n"))

      dict_of_lists[listname]={}
      break
    except ValueError:
      print "=>Please enter a valid name.\n"

  print "=>You added a new list named %s.\n" % (listname)
def printlists():
  for lists in dict_of_lists:
    return "-"+lists
def addproduct():
  while True:
    try:
      reachlistname=input("=>Name of the list you want to add a product,Available lists are these:\n %s \nPlease enter one:\n" % (printlists()))
      break
    except ValueError:
      print "=>Please enter a valid list name.\n"
  while True:
    try:
      productname=raw_input("=>Name of the product:\n")
      break
    except ValueError:
      print "=>Please enter a valid name.\n"
  while True:
    try:
      productprice=input("=>Price of the product:\n")
      if isinstance(float(productprice),float):
        break
    except ValueError:
          print "=>Please enter a valid number.\n"
  while True:
    try:
      productcount=input("=>Amount of the product:\n")
      if isinstance(int(productcount),int):
        break
    except ValueError:
      print "=>Please enter a valid number.\n"
  dict_of_lists[reachlistname][productname]={"price":productprice,"count":productcount}
  dict_of_lists[reachlistname]={productname:{"price":productprice,"count":productcount}}
allevents="1-Add a list"+" 2-Add a product to a list"  
def eventtochoose():
  while True:
    try:
      event=raw_input("=>What would you like to do? Here are the all things you can do:\n %s\nPlease enter the number before the thing you want to do:" % (allevents))
      if  not isinstance(int(event),int):
        print "\n=>Please enter a number.\n"
      else:
        if event==1:
          addlist()
          break
        elif event==2:
          addproduct()
          break

    except ValueError:
      print "\n=>Please enter a valid input.\n "

while True:
  print "%s" % ("\n"*100)
  eventtochoose()

Illuminate\Http\UploadedFile延伸Symfony\Component\HttpFoundation\File\UploadedFile延伸Symfony\Component\HttpFoundation\File\File延伸SplFileInfo

答案 1 :(得分:0)

我正在使用Laravel版本6,其他解决方案无效。以下为我工作:

$file = request()->fileinput;      // <input type="file" name="fileinput" />
$content = file_get_contents($file);
$json = json_decode($content, true);

答案 2 :(得分:-1)

我这样做了,它对我有用

Input::file('fileinput')->move(public_path(), 'fortinet_logs.json');
$string = file_get_contents(public_path().'/fortinet_logs.json');
$json = json_decode($string, true);