2013-05-19 43 views
24

Dưới đây là một ví dụ về mã Json tôi:JSON chuỗi từ Gson: loại bỏ dấu ngoặc kép

array("id" => 0, "navn" => "Vind telefon", "udgiver" => "Telia", "beskrivelse" => utf8_encode("Vind en Iphone 5 ved at gætte 1 spørgsmål"), "tilmeldte" => 89, "startdate" => "10-04-2013", "enddate" => "30-06-2013"), 

Nếu tôi in ra navn thuộc tính trong chương trình của tôi tôi nhận được kết quả như sau:

"Vind Telefon" 

Như bạn có thể thấy nó giữ các dấu ngoặc kép.

Có cách nào tôi có thể tránh điều đó không?

Mã của tôi đầy đủ:

<?php 
header('Content-Type: json; charset=utf-8'); 
$lottery = array(
    array("id" => 0, "navn" => "Vind telefon", "udgiver" => "Telia", "beskrivelse" => utf8_encode("Vind en Iphone 5 ved at gætte 1 spørgsmål"), "tilmeldte" => 89, "startdate" => "10-04-2013", "enddate" => "30-06-2013"), 
    array("id" => 1, "navn" => "Fri taletid", "udgiver" => "Telmore", "beskrivelse" => utf8_encode("Vind et års forbrug af fri data og fri taletid"), "tilmeldte" => 701, "startdate" => "03-03-2013", "enddate" => "10-06-2013"), 
    array("id" => 2, "navn" => "Vind 5000 kr.", "udgiver" => "Bilka", "beskrivelse" => utf8_encode("Vind et gavekort på 5000 kr til bilka."), "tilmeldte" => 101, "startdate" => "10-05-2013", "enddate" => "01-07-2013"), 
    array("id" => 3, "navn" => "Fri vin", "udgiver" => "Føtex", "beskrivelse" => utf8_encode("Vind et års forbrug af vin"), "tilmeldte" => 391, "startdate" => "01-04-2013", "enddate" => "10-07-2013"), 
    array("id" => 4, "navn" => "Vind et TV", "udgiver" => "El-giganten", "beskrivelse" => utf8_encode("Vind et 60\" LED tv til stuen"), "tilmeldte" => 22, "startdate" => "01-04-2013", "enddate" => "22-07-2013"), 
    array("id" => 5, "navn" => "Vind en telefon", "udgiver" => "Samsung", "beskrivelse" => utf8_encode("Vind en Galaxy S4 4g telefon"), "tilmeldte" => 784, "startdate" => "10-04-2013", "enddate" => "30-07-2013"), 
    array("id" => 6, "navn" => "Gratis Vand", "udgiver" => "Aqua D'or", "beskrivelse" => utf8_encode("Vind et helt års forbrug af vand og dansk vand"), "tilmeldte" => 345, "startdate" => "01-03-2013", "enddate" => "18-07-2013"), 
    array("id" => 7, "navn" => "Fri Adwords", "udgiver" => "Google", "beskrivelse" => utf8_encode("Vind adwords kuponner til en værdi af 10.000"), "tilmeldte" => 22, "startdate" => "10-02-2013", "enddate" => "22-08-2013"), 
    array("id" => 8, "navn" => "Morgenmads produkter", "udgiver" => "Kellogs", "beskrivelse" => utf8_encode("Vind et helt års forbrug af kellogs produkter"), "tilmeldte" => 111, "startdate" => "01-05-2013", "enddate" => "10-09-2013"), 
    array("id" => 9, "navn" => utf8_encode("Vind tøj for 10.000"), "udgiver" => "Jack & Jones", "beskrivelse" => utf8_encode("Vind et gavekort til Jack & Jones på 10.000"), "tilmeldte" => 33, "startdate" => "03-05-2013", "enddate" => "01-10-2013"), 
); 
//id, navn, udgiver, beskrivelse, tilmeldte, startdate, slutdate 
echo json_encode($lottery); 
?> 

Mã java in nó ra:

 String discription = arrayListing.get(i).getAsJsonObject().get("beskrivelse").toString(); 
    System.out.println(discription); 

đầy đủ mã java My

public static void main(String[] args) { 
    String result = null; 
    BufferedReader reader = null; 
    try { 
     URL url = new URL("http://www.marcrasmussen.dk/json.php"); 
     reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8")); 
     for (String line; (line = reader.readLine()) != null;) { 
      System.out.println(line); 
      result = line; 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (reader != null) { 
      try { 
       reader.close(); 
      } catch (IOException e) { 
       // Ignore 
      } 
     } 
    } 

    JsonElement root = new JsonParser().parse(result); 


    HashMap<Integer,CompetetionObject> comMap = new HashMap<>(); 

    JsonArray arrayListing = root.getAsJsonArray(); 
    for (int i = 0; i < root.getAsJsonArray().size(); i++) { 
     int id = Integer.parseInt(arrayListing.get(i).getAsJsonObject().get("id").toString()); 
     String name = arrayListing.get(i).getAsJsonObject().get("navn").toString(); 
     String provider = arrayListing.get(i).getAsJsonObject().get("udgiver").toString(); 
     String discription = arrayListing.get(i).getAsJsonObject().get("beskrivelse").toString(); 
     int signedUp = Integer.parseInt(arrayListing.get(i).getAsJsonObject().get("tilmeldte").toString()); 
     String startDate = arrayListing.get(i).getAsJsonObject().get("startdate").toString(); 
     String endDate = arrayListing.get(i).getAsJsonObject().get("enddate").toString(); 
     CompetetionObject co = new CompetetionObject(id, name, provider, discription, signedUp, startDate, endDate); 
     String day = startDate.substring(1, 3); 
     String month = startDate.substring(5,6); 
     String year = startDate.substring(7,startDate.length()-1); 
     DateTime d = new DateTime(year+"-"+month+"-"+day); 
     System.out.println(discription); 
     comMap.put(id, co); 
    } 
} 

Sản lượng của m y chương trình Java:

[{"id":0,"navn":"Vind telefon","udgiver":"Telia","beskrivelse":"Vind en Iphone 5 ved at g\u00e6tte 1 sp\u00f8rgsm\u00e5l","tilmeldte":89,"startdate":"10-04-2013","enddate":"30-06-2013"},{"id":1,"navn":"Fri taletid","udgiver":"Telmore","beskrivelse":"Vind et \u00e5rs forbrug af fri data og fri taletid","tilmeldte":701,"startdate":"03-03-2013","enddate":"10-06-2013"},{"id":2,"navn":"Vind 5000 kr.","udgiver":"Bilka","beskrivelse":"Vind et gavekort p\u00e5 5000 kr til bilka.","tilmeldte":101,"startdate":"10-05-2013","enddate":"01-07-2013"},{"id":3,"navn":"Fri vin","udgiver":null,"beskrivelse":"Vind et \u00e5rs forbrug af vin","tilmeldte":391,"startdate":"01-04-2013","enddate":"10-07-2013"},{"id":4,"navn":"Vind et TV","udgiver":"El-giganten","beskrivelse":"Vind et 60\" LED tv til stuen","tilmeldte":22,"startdate":"01-04-2013","enddate":"22-07-2013"},{"id":5,"navn":"Vind en telefon","udgiver":"Samsung","beskrivelse":"Vind en Galaxy S4 4g telefon","tilmeldte":784,"startdate":"10-04-2013","enddate":"30-07-2013"},{"id":6,"navn":"Gratis Vand","udgiver":"Aqua D'or","beskrivelse":"Vind et helt \u00e5rs forbrug af vand og dansk vand","tilmeldte":345,"startdate":"01-03-2013","enddate":"18-07-2013"},{"id":7,"navn":"Fri Adwords","udgiver":"Google","beskrivelse":"Vind adwords kuponner til en v\u00e6rdi af 10.000","tilmeldte":22,"startdate":"10-02-2013","enddate":"22-08-2013"},{"id":8,"navn":"Morgenmads produkter","udgiver":"Kellogs","beskrivelse":"Vind et helt \u00e5rs forbrug af kellogs produkter","tilmeldte":111,"startdate":"01-05-2013","enddate":"10-09-2013"},{"id":9,"navn":"Vind t\u00f8j for 10.000","udgiver":"Jack & Jones","beskrivelse":"Vind et gavekort til Jack & Jones p\u00e5 10.000","tilmeldte":33,"startdate":"03-05-2013","enddate":"01-10-2013"}] 
"Vind en Iphone 5 ved at gætte 1 spørgsmål" 
"Vind et års forbrug af fri data og fri taletid" 
"Vind et gavekort på 5000 kr til bilka." 
"Vind et års forbrug af vin" 
"Vind et 60\" LED tv til stuen" 
"Vind en Galaxy S4 4g telefon" 
"Vind et helt års forbrug af vand og dansk vand" 
"Vind adwords kuponner til en værdi af 10.000" 
"Vind et helt års forbrug af kellogs produkter" 
"Vind et gavekort til Jack & Jones på 10.000" 
+6

Đó không phải là JSON. –

+2

Và nó không phải là Java. –

+1

Không. Nó không phải Java vì là PHP. –

Trả lời

56

Nó không ghi nhận đúng, nhưng JsonElement#toString() giúp bạn một chuỗi đại diện cho yếu tố JSON và sẽ là thích hợp để tái tạo serialization JSON. Những gì bạn muốn là JsonElement#getAsString(). Điều này sẽ ném một lỗi nếu bạn không nhìn vào một chuỗi, nhưng nếu bạn đang có, bạn sẽ nhận được giá trị chuỗi.

Dưới đây là một chương trình thử nghiệm để chứng minh:

import com.google.gson.JsonElement; 
import com.google.gson.JsonParser; 
public class Test { 
    public static void main(String[] args) { 
     String in = "{\"hello\":\"world\"}"; 
     System.out.println(in); 
     JsonElement root = new JsonParser().parse(in); 
     System.out.println(root.getAsJsonObject().get("hello").toString()); 
     System.out.println(root.getAsJsonObject().get("hello").getAsString()); 
    } 
} 

Và sản lượng của nó:

{"hello":"world"} 
"world" 
world 
+0

Câu trả lời của chúng tôi đều có liên quan. Tuy nhiên, bạn trả lời đơn giản hơn tôi. – Cyril

+0

:-) JAXB là tuyệt vời, nhưng tôi nghĩ tôi sẽ làm 'discription.substring (1, discription.length() - 1)' trước khi chuyển đổi JSON thành XML chỉ cho đầu ra. –

+0

@NathanielWaisbrot oh thanx rất nhiều cho cuộc biểu tình của bạn !!! tôi đã tìm kiếm giải pháp từ ba ngày qua. –

Các vấn đề liên quan