dpd20130
dpd20130
2017-09-18 18:36

将文本解析为json

已采纳

I have a txt file that looks like that:

1 - 10
2 - 20
3 - 30

How can I make a json array that looks like that:

{
  "item": "1",
  "value": "10"
},

{
  "item": "2",
  "value": "20"
},

{
  "item": "3",
  "value": "30"
},

Thanks!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • duanben1909 duanben1909 4年前

    You need to read line by line from the text file and explode the line with delimiter '-'. Then create an array which can be converted to json.

    // Open the file to read data.
    $fh = fopen('student.txt','r');
    // define an eampty array
    $data = array();
    // read data
    while ($line = fgets($fh)) {
        // if the line has some data
       if(trim($line)!=''){
           // explode each line data 
           $line_data = explode('-',$line);
           // push data to array
           //array_push($data,array('item'=>trim($line_data[0]),'value'=>trim($line_data[1])));
           $data[]=array('item'=>trim($line_data[0]),'value'=>trim($line_data[1]));
       }
    }
    fclose($fh);
    // json encode the array
    echo $json_data = json_encode($data);
    

    Output:

    [{"item":"1","value":"10"},
    {"item":"2","value":"20"},
    {"item":"3","value":"30"}]
    
    点赞 评论 复制链接分享
  • doupi1532 doupi1532 4年前

    another method solution ::

    <?php
    
    // the list in the txt file 
    $str = "1 - 10
            2 - 20
            3 - 30";
    
    
    $so = explode(" ", $str);
    
    
    
    $cleanedvalues = array();
    
    foreach ($so as $key => $value) {
        if ($value != "") {
            $cleanedvalues[] = $value;
        }
    }
    
    
    $arrval = array();
    
    foreach ($cleanedvalues as $key => $value) {
        if ($value == "-") {
    
        } else {
            $arrval[] = $value;
        }
    }
    
    
    
    $tab_keys   = array();
    $tab_values = array();
    foreach ($arrval as $key => $value) {
        if ($key % 2 == 0) {
            $tab_keys[] = $value;
        } else {
            $tab_values[] = $value;
        }
    
    }
    
    // add the item and value data 
    $lan = array();
    for ($i = 0; $i < sizeof($tab_keys); $i++) {
        $lan[] = array(
            'item' => trim($tab_keys[$i]),
            'value' => trim($tab_values[$i])
        );
    }
    
    // the final result 
    echo json_encode($lan);
    ?>
    
    点赞 评论 复制链接分享
  • dongweihuan8610 dongweihuan8610 4年前
    #include file with data
    $arg = file('text.txt');
    
    #create array for our json
    $res = [];
    
    #start the cycle
    foreach ($arg as $k => $v) {
    
        #convert string to array.
        $arr[] = explode(' - ', $v);
    
        #take first element
        $res[$k]['item'] = current($arr[$k]);
    
        #take second element and cut wrapp with (int)
        $res[$k]['value'] = (int)next($arr[$k]);
    }
    
    #option "JSON_PRETTY_PRINT" for displaying the format text
    $res = json_encode($res, JSON_PRETTY_PRINT);
    
    #print result
    echo '<pre>';
    print_r($res);
    echo '</pre>';
    

    Our result

    [
        {
            "item": "1",
            "value": 10
        },
        {
            "item": "2",
            "value": 20
        },
        {
            "item": "3",
            "value": 30
        },
        {
            "item": "5",
            "value": 33
        },
        {
            "item": "4",
            "value": 3534
        }
    ]
    
    点赞 评论 复制链接分享

为你推荐