2014-04-13 47 views
0

我有這樣的代碼,我想用PHP以數組格式讀取它。在php中閱讀此代碼

"Vehicle" 
{ 
"WheelsPerAxle"  "2" 
"Body" 
{ 
    "CounterTorqueFactor" "0.9" 
    "MassCenterOverride" "0 -3 13" 
    "MassOverride"   "1467" 
    "AddGravity"   "0.7" 
    "MaxAngularVelocity" "720" 
} 
"Engine" 
{ 
    "HorsePower"   "301" 
    "MaxRPM"    "4200" 
    "MaxSpeed"    "95" 
    "MaxReverseSpeed"  "28" 
    "AutobrakeSpeedGain" "1.1" 
    "AutobrakeSpeedFactor" "3" 
    "Autotransmission"  "0" 
    "AxleRatio"    "4.56" 
    "Gear"     "3.2" 
    "Gear"     "2.4" 
    "Gear"     "1.5" 
    "Gear"     "1" 
    "Gear"     "0.84" 

    "ShiftUpRPM"   "3800" 
    "ShiftDownRPM"   "1600" 
} 
"Steering" 
{ 
    "DegreesSlow"      "40" 
    "DegreesFast"      "12" 
    "DegreesBoost"      "11" 
    "FastDampen"      "0" 
    "SteeringExponent"     "1.4" 
    "SlowCarSpeed"      "12" 
    "FastCarSpeed"      "60" 
    "SlowSteeringRate"     "3" 
    "FastSteeringRate"     "2" 
    "SteeringRestRateSlow"    "3" 
    "SteeringRestRateFast"    "2" 
    "TurnThrottleReduceSlow"   "0.01" 
    "TurnThrottleReduceFast"   "0.6" 
    "BrakeSteeringRateFactor"   "3" 
    "ThrottleSteeringRestRateFactor" "2" 
    "BoostSteeringRestRateFactor"  "1.7" 
    "BoostSteeringRateFactor"   "1.7" 

    "PowerSlideAccel"     "250" 

    "SkidAllowed"      "1" 
    "DustCloud"       "1" 
} 
"Axle" 
{ 
    "Wheel" 
    { 
     "Radius"      "15" 
     "Mass"       "270" 
     "Inertia"      "0" 
     "Damping"      "0.3" 
     "RotDamping"     "0.2" 
     "Material"      "jeeptire" 
     "SkidMaterial"     "slidingrubbertire" 
     "BrakeMaterial"     "brakingrubbertire" 
    } 
    "Suspension" 
    { 
     "SpringConstant"    "90" 
     "SpringDamping"     "0.5" 
     "StabilizerConstant"   "0" 
     "SpringDampingCompression"  "7" 
     "MaxBodyForce"     "10" 
    } 
    "TorqueFactor"      "0.95" 
    "BrakeFactor"      "0.4" 
} 
"Axle" 
{ 
    "Wheel" 
    { 
     "Radius"      "15" 
     "Mass"       "230" 
     "Inertia"      "0" 
     "Damping"      "0" 
     "RotDamping"     "0" 
     "Material"      "jeeptire" 
     "SkidMaterial"     "slidingrubbertire" 
     "BrakeMaterial"     "brakingrubbertire" 
    } 
    "Suspension" 
    { 
     "SpringConstant"    "70" 
     "SpringDamping"     "0.5" 
     "StabilizerConstant"   "0" 
     "SpringDampingCompression"  "7" 
     "MaxBodyForce"     "15" 
    } 
    "TorqueFactor"      "0" 
    "BrakeFactor"      "0.6" 
} 
} 

    "Vehicle_Sounds" 
    { 
"Gear" 
{ 
    "Max_Speed"    "0.06" 
    "Speed_Approach_Factor" "1" 
} 
"Gear" 
{ 
    "Max_Speed"    "0.25" 
    "Speed_Approach_Factor" "0.08" 
} 
"Gear" 
{ 
    "Max_Speed"    "0.35" 
    "Speed_Approach_Factor" "0.05" 
} 
"Gear" 
{ 
    "Max_Speed"    "0.55" 
    "Speed_Approach_Factor" "0.035" 
} 
"Gear" 
{ 
    "Max_Speed"    "1" 
    "Speed_Approach_Factor" "0.01" 
} 
"State" 
{ 
    "Name"  "SS_SHUTDOWN_WATER" 
    "Sound"  "atv_stall_in_water" 
    "Min_Time" "0" 
} 
"State" 
{ 
    "Name"  "SS_GEAR_2_RESUME" 
    "Sound"  "vehicles\tdmcars\focusrs\third.wav" 
    "Min_Time" "0.5" 
} 
"State" 
{ 
    "Name"  "SS_START_WATER" 
    "Sound"  "atv_start_in_water" 
    "Min_Time" "0" 
} 
"State" 
{ 
    "Name"  "SS_GEAR_3_RESUME" 
    "Sound"  "vehicles\tdmcars\focusrs\fourth_cruise.wav" 
    "Min_Time" "0.5" 
} 
"State" 
{ 
    "Name"  "SS_REVERSE" 
    "Sound"  "vehicles\tdmcars\focusrs\rev.wav" 
    "Min_Time" "0.5" 
} 
"State" 
{ 
    "Name"  "SS_IDLE" 
    "Sound"  "vehicles\tdmcars\focusrs\idle.wav" 
    "Min_Time" "0" 
} 
"State" 
{ 
    "Name"  "SS_GEAR_1_RESUME" 
    "Sound"  "vehicles\tdmcars\focusrs\first.wav" 
    "Min_Time" "0.5" 
} 
"State" 
{ 
    "Name"  "SS_GEAR_4" 
    "Sound"  "vehicles\tdmcars\focusrs\fourth_cruise.wav" 
    "Min_Time" "0.5" 
} 
"State" 
{ 
    "Name"  "SS_GEAR_1" 
    "Sound"  "vehicles\tdmcars\focusrs\first.wav" 
    "Min_Time" "0.5" 
} 
"State" 
{ 
    "Name"  "SS_SLOWDOWN_HIGHSPEED" 
    "Sound"  "vehicles\tdmcars\focusrs\throttle_off.wav" 
    "Min_Time" "0" 
} 
"State" 
{ 
    "Name"  "SS_SHUTDOWN" 
    "Sound"  "vehicles\tdmcars\slsamg\engineoff.mp3" 
    "Min_Time" "0" 
} 
"State" 
{ 
    "Name"  "SS_START_IDLE" 
    "Sound"  "vehicles\tdmcars\slsamg\enginestart.mp3" 
    "Min_Time" "0.3" 
} 
"State" 
{ 
    "Name"  "SS_GEAR_4_RESUME" 
    "Sound"  "vehicles\tdmcars\focusrs\fourth_cruise.wav" 
    "Min_Time" "0.5" 
} 
"State" 
{ 
    "Name"  "SS_GEAR_3" 
    "Sound"  "vehicles\tdmcars\focusrs\third.wav" 
    "Min_Time" "0.5" 
} 
"State" 
{ 
    "Name"  "SS_GEAR_0_RESUME" 
    "Sound"  "vehicles\tdmcars\focusrs\first.wav" 
    "Min_Time" "0.75" 
} 
"State" 
{ 
    "Name"  "SS_GEAR_0" 
    "Sound"  "vehicles\tdmcars\focusrs\idle.wav" 
    "Min_Time" "0.08" 
} 
"State" 
{ 
    "Name"  "SS_GEAR_2" 
    "Sound"  "vehicles\tdmcars\focusrs\second.wav" 
    "Min_Time" "0.5" 
} 
"State" 
{ 
    "Name"  "SS_SLOWDOWN" 
    "Sound"  "vehicles\tdmcars\focusrs\idle.wav" 
    "Min_Time" "0" 
} 
"CrashSound" 
{ 
    "Min_Speed"   "350" 
    "Min_Speed_Change" "250" 
    "Sound"    "atv_impact_medium" 
    "Gear_Limit"  "1" 
} 
"CrashSound" 
{ 
    "Min_Speed"   "450" 
    "Min_Speed_Change" "350" 
    "Sound"    "atv_impact_heavy" 
    "Gear_Limit"  "0" 
} 

"Skid_LowFriction"  "" 
"Skid_NormalFriction" "" 
"Skid_HighFriction"  "" 
} 

我不知道我應該怎麼開始

按鍵都是一樣的

有人可以給我如何分析這樣一個文件的提示?

在此先感謝

+0

的[解析JSON字符串內容爲PHP數組(http://stackoverflow.com/questions/13600024/parse-json-string-contents-into-php-array) –

+0

可能重複它只是看起來像JSON,但它不是JSON。它根本無效。沒有像':','''等符號,它們必須劃分json代碼。 –

+0

這不是JSON - 沒有足夠的標點符號:等 – IMSoP

回答

-1

我看到這是一個LUA陣列您嘗試讀取,所以你需要一個Lua> PHP轉換的東西。這裏有一個關於lua到php的信息。

info lua to php

希望這會幫助你。

+0

這不是Lua數組,它缺少「=」符號 – Schollii

0

看起來你應該爲這種格式編寫自己的解析器。你可以嘗試找到一個解析器生成器來從一些中間語法生成一個解析器,或者你可以從頭開始編寫它。我認爲從頭開始寫這個情況是可行的。

我已經給它一個鏡頭併爲你實現了一個演示解析器,解析器只是你需要的一部分。評論應該足以說明你的入門。

你需要特別注意的是,當你開始處理花括號時,你需要遞歸調用。祝你好運!

<?php 
// simple demo parser that will parse tokens surrounded by " from a file that 
// only contains whitespace in the form of spaces or newlines aside from these 
// tokens 
// For example something like this: 
/* 
"hello" 

    "this" 

    "is" 

    "a" 



"Test" 
    "10.9" 
*/ 


// parse a token from the string 
// assume opening " has already been encountered 
// note the pass by reference! 
function parse_token($textData, &$index, &$token) { 
    $token = ""; 
    $length = strlen($textData); 
    while ($index < $length) { 

     // get next character 
     $char = $textData{$index}; 

     // increment for next round 
     // do this here because we want to increment before breaking 
     // out of the loop 
     $index++; 

     // anything that is not " is part of the token 
     if ("\"" != $char) { 
      $token .= $char; 
     } else { 
      break; 
     } 

    } 
} 

// main function to do the parsing 
// note the pass by reference! 
function do_parse($textData, &$index, &$output) { 
    $output = []; 
    $char = NULL; 
    $length = strlen($textData); 
    $token = NULL; 
    while ($index < $length) { 

     // get next character 
     $char = $textData{$index}; 

     // if we encounter an opening " 
     if ("\"" == $char) { 
      // parse the rest as a token, and store it in our output array 
      $output[] = parse_token($textData, ++$index, $token); 
     } else if (" " == $char || "\n" == $char) { 
      // skip whitespace, go on to next char 
      $index++; 
     } else { 
      throw new Exception('unexpected character ' . $char); 
     } 
    } 
} 

// read file into string 
$data = file_get_contents ("data.txt"); 

// perform actual parsing 
$index = 0; 
do_parse($data, $index, $parsed); 

// print result 
print_r($parsed);