服务器端 - 创建JSON响应

时间:2013-07-03 21:44:10

标签: java php json

关注所有有才华的程序员,首先,让我从保存开始,我拥有JSON的ZERO经验,而且没有太多的服务器端编码经验。这是我想要做的:

我想在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"},
    ]
}


?>

1 个答案:

答案 0 :(得分:1)

我建议你可以从very simple php tutorial

开始

但是,这可能会让你开始:

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

您现在应该可以访问http://yourserver/path/yourphp.php?keyword=A获取one result.http://yourserver/path/yourphp.php?keyword=B获取a different one!