0

注意所有有才华的编码人员,首先,让我从保存开始,我对 JSON 的经验为零,而服务器端编码经验不多。这是我正在尝试做的事情:

我想用 Java 或 PHP 构建一个非常简单的后端服务器,它响应 get 命令并根据关键字返回 2 个不同的 JSON 结果(即,如果关键字以 A 开头或为空,则返回一个结果。如果它以 B 开头,返回一个不同的)。再一次,我不知道如何解决这个问题,如果有人能为我解决这个问题,我将不胜感激。这不是家庭作业,我只是想学习和掌握一些技能。如果有人真的喜欢编码并且有空闲时间,请帮助我了解这是如何完成的。任何帮助是极大的赞赏。真的。提前非常感谢大家。

这是我尝试过的。我最终想在搜索关键字时发送如下所示的 JSON onbect。

 <?php


    $data = $_GET['keyword'];


      $nullOrA = substr($data, 0)

     if($nullOrA != "A" || "B" || "C"){
    echo "Cannot find anything related to searched Keyword"; 
    }

  if($nullOrA == "A"){
    echo json_encode($candStr); 
}

if($nullOrA == "B"){
    echo json_encode($jobStr); 
}

else if($nullOrA == "C"){
    echo json_encode($companyStr); 
}

//JSON object
 $Response Object:

$candStr = {"candidates": [
        {"lastName":"Doe" , "firstName":"John" , "mailingState":"IN" , "availDate:":"08/15/2013" , "matchPct":"92%", "division":"temps" , "branch":"Branch AA"},
        {"lastName":"Smith" , "firstName":"Lin" , "mailingState":"CA" , "availDate:":"08/15/2013" , "matchPct":"92%", "division":"gloabl" , "branch":"Branch AB"},
        {"lastName":"Memory" , "firstName":"James" , "mailingState":"CA" , "availDate:":"08/15/2013" , "matchPct":"92%", "division":"accountemps" , "branch":"Branch AA"},
        {"lastName":"App" , "firstName":"Tom" , "mailingState":"CA" , "availDate:":"08/15/2013" , "matchPct":"92%", "division":"accountemps" , "branch":"Branch AO"},
        {"lastName":"World" , "firstName":"Yeah" , "mailingState":"CA" , "availDate:":"08/15/2013" , "matchPct":"92%", "division":"accountemps" , "branch":"Branch CA"},
        {"lastName":"Timmy" , "firstName":"Hungry" , "mailingState":"NY" , "availDate:":"08/15/2013" , "matchPct":"92%", "division":"accoun" , "branch":"Branch AA"},
        {"lastName":"Hello" , "firstName":"Luke" , "mailingState":"CA" , "availDate:":"08/15/2013" , "matchPct":"92%", "division":"accountemps" , "branch":"Branch AA"},
        {"lastName":"James" , "firstName":"Ashley" , "mailingState":"CA" , "availDate:":"08/15/2013" , "matchPct":"92%", "division":"accountemps" , "branch":"Branch AD"},
        {"lastName":"Duck" , "firstName":"MAl" , "mailingState":"TN" , "availDate:":"08/15/2013" , "matchPct":"92%", "division":"accountemps" , "branch":"Branch AA"},
    ],
$jobStr ="jobOrders": [
        {"name":"Lawyer" , "company":"DHI" , "availDate":"08/15/2013" , "matchPct":"92%", "division":"Business" , "branch":"Branch NA"},
        {"name":"Accountant" , "company":"HTG" , "availDate":"08/15/2013" , "matchPct":"92%", "division":"accountemps" , "branch":"Branch AA"},
        {"name":"ncurses_addchstr(s)" , "company":"HTB" , "availDate":"08/15/2013" , "matchPct":"92%", "division":"accountemps" , "branch":"Branch OA"},
        {"name":"Accountant" , "company":"KNM" , "availDate":"08/15/2013" , "matchPct":"92%", "division":"global" , "branch":"Branch AA"},
        {"name":"Lead" , "company":"KOL" , "availDate":"08/15/2013" , "matchPct":"92%", "division":"accountemps" , "branch":"Branch AU"},
        {"name":"Accountant" , "company":"KNM" , "availDate":"08/15/2013" , "matchPct":"92%", "division":"accountemps" , "branch":"Branch AA"},
        {"name":"Dev" , "company":"KNM" , "availDate":"08/15/2013" , "matchPct":"92%", "division":"accountemps" , "branch":"Branch CA"},
        {"name":"Accountant" , "company":"KNM" , "availDate":"08/15/2013" , "matchPct":"92%", "division":"Native" , "branch":"Branch AA"},
        {"name":"Analyst" , "company":"KNM" , "availDate":"08/15/2013" , "matchPct":"92%", "division":"accountemps" , "branch":"Branch PO"},
    ],
$companyStr = "companies": [
        {"name":"Fruit" , "mailingState":"IN" , "matchPct":"92%", "division":"temps" , "branch":"Branch RA"},
        {"name":"Insect" , "mailingState":"CA" , "matchPct":"92%", "division":"accountemps" , "branch":"Branch AA"},
        {"name":"Color" , "mailingState":"CA" , "matchPct":"92%", "division":"accountemps" , "branch":"Branch AA"},
        {"name":"Doe" , "mailingState":"CA" , "matchPct":"92%", "division":"account" , "branch":"Branch AA"},
        {"name":"Doe" , "mailingState":"YH" , "matchPct":"92%", "division":"global" , "branch":"Branch AY"},
        {"name":"Doe" , "mailingState":"CA" , "matchPct":"92%", "division":"temps" , "branch":"Branch MN"},
        {"name":"Doe" , "mailingState":"LK" , "matchPct":"92%", "division":"accountemps" , "branch":"Branch AA"},
        {"name":"Doe" , "mailingState":"CA" , "matchPct":"92%", "division":"accountemps" , "branch":"Branch LI"},
        {"name":"Doe" , "mailingState":"BH" , "matchPct":"92%", "division":"account" , "branch":"Branch AA"},
        {"name":"Doe" , "mailingState":"CA" , "matchPct":"92%", "division":"accountemps" , "branch":"Branch AA"},
        {"name":"Doe" , "mailingState":"DV" , "matchPct":"92%", "division":"US" , "branch":"Branch YH"},
    ]
}


?>
4

1 回答 1

1

我建议你可以从一个非常简单的 php 教程开始

但是,这可能会让您入门:

<?php
if ($_GET["keyword"]=="B") {
    echo "a different one!";
} else {
    echo "one result.";
}

您现在应该可以访问http://yourserver/path/yourphp.php?keyword=Agetone result.http://yourserver/path/yourphp.php?keyword=Bgeta different one!

于 2013-07-03T21:49:08.450 回答