2017-06-26 14 views
0

他のフィールドを正しく取得できますが、 "title"または "title_full"の値を解析できます。私はいつも空の文字列を受け取ります。私はorg.jsonライブラリを使用しています。これがjsonです。トリックは何ですか?このjsonフィールドの解析に失敗しました

try { 
       title = jsonDoc.getString("title_full"); 
      } catch (JSONException e) { 
       log.info("no full title: " + docString); 
      } 

{ 
    "organizations": [], 
    "uuid": "d0adc516c9012113774557365f9847da99b228e7", 
    "thread": { 
    "site_full": "www.fark.com", 
    "main_image": "http://img.fark.net/images/cache/orig/5/51/fark_514Jh7VFpynQw4MyN2xcK1jwCxk.png?t=RQrnhq8EGZiUuElMitgLOQ&f=1488776400", 
    "site_section": "http://www.fark.com/discussion/", 
    "section_title": "FARK.com: Discussion links", 
    "url": "http://www.fark.com/comments/9500577/I-want-to-support-work-that-NY-Times-Washington-Post-are-doing-I-can-only-afford-one-subscription-Who-do-you-recommend-I-throw-my-support-to?cpp=1", 
    "country": "US", 
    "domain_rank": 3382, 
    "title": "(9500577) I want to support the work that the NY Times and Washington Post are doing. I can only afford one subscription. Who do you recommend I throw my support to?", 
    "performance_score": 0, 
    "site": "fark.com", 
    "participants_count": 31, 
    "title_full": "FARK.com: (9500577) I want to support the work that the NY Times and Washington Post are doing. I can only afford one subscription. Who do you recommend I throw my support to?", 
    "spam_score": 0.0, 
    "site_type": "discussions", 
    "published": "2017-03-03T12:00:00.000+02:00", 
    "replies_count": 2, 
    "uuid": "67213179a24931106e75cd588386bd30fb3bbdc8" 
    }, 
    "author": "EbolaNYC", 
    "url": "http://www.fark.com/comments/9500577/I-want-to-support-work-that-NY-Times-Washington-Post-are-doing-I-can-only-afford-one-subscription-Who-do-you-recommend-I-throw-my-support-to?cpp=1#c107765048", 
    "ord_in_thread": 1, 
    "title": "", 
    "locations": [], 
    "entities": { 
    "persons": [], 
    "locations": [], 
    "organizations": [] 
    }, 
    "highlightText": "", 
    "language": "english", 
    "persons": [], 
    "text": "dionysusaur : Either the NY Post or the WA Times.\nOnly asshats read the NY Post.", 
    "external_links": [], 
    "published": "2017-03-03T15:58:00.000+02:00", 
    "crawled": "2017-03-03T17:05:26.049+02:00", 
    "highlightTitle": "", 
    "social": { 
    "gplus": {"shares": 0}, 
    "pinterest": {"shares": 0}, 
    "vk": {"shares": 0}, 
    "linkedin": {"shares": 0}, 
    "facebook": {"likes": 0, "shares": 0, "comments": 0}, 
    "stumbledupon": {"shares": 0} 
    } 
} 
+0

あなたはjsonをインデントしますか?構造を分析する方が簡単です。 – AxelH

+1

ネストしたデータ構造にアクセスする必要があります.titleとfull_titleは最初のレベルにはありません。ネストされたデータ構造にアクセスする必要があります。 – luk2302

+0

字下げで、 'title'または' title_full'が 'jsonDoc'にないことは明らかです。それらは' thread'オブジェクトにあります – AxelH

答えて

1

IはJSONコードをフォーマットした後、問題が顕在化: title_fullthreadノード内のみ利用可能であり、そして非空titleのみthreadノード内でもあります。したがって、最初にthreadノードにアクセスし、そのノード内でtitletitle_fullにアクセスする必要があります。 org.jsonライブラリを使用して

、あなたはこのようなフィールドにアクセスすることができます

String fullTitle = jsonDoc.getJSONObject("thread").getString("title_full"); 
+0

スレッド内の埋め込みjson構造にアクセスするにはどうすればいいですか? – user697911

+0

'' jsonDoc.getJSONObject( "thread")を試してください。getString( "title_full") '' – cello

+0

ありがとうございます。それは本当にクールです。 – user697911

0

あなたはJSONを見ている場合は、「タイトル」と「title_full」フィールドがでていることがわかりますスレッドフィールド。 そのフィールドを読んでから、新しいjsonObjectにファイルを解析すると、それらを取得できるはずです。

{ 
    { 
    "main": { 
     "key": "value", 
      }, 
    }, 
} 

をので、まずメインJSONし、キーをフェッチ:

2

あなたのJSONは、以下のように思えます。次のように

コードは次のようにする必要があります:

String something = jsonDoc.get("main").get("key").toString(); 

あなたのJSONに2つのタイトルの値がありますが、あなたがフェッチする前に必要があるタイトルチェックしてください。

+0

これは、私にとっては最も完全で正式な答えです。 'jsonDoc.get(" main ")'が返すものについて何らかの精度を加えるだけでよいのです。そして、実際には 'jsonDoc'は、同じ型のstrucutreになるでしょう。 – AxelH

関連する問題