Json简单智能图书馆

时间:2014-03-16 13:04:19

标签: json parsing

我正在使用Json简单库来处理twitter数据。然而根据一些json stansards,twitter数据无效。然后我转向Json智能库,但它显示了同样的问题。 Twitter数据具有键值对,使得该对可以包含许多双重投入的逗号,例如值="有些g" uy"。因此,这会导致违反json标准。即使某些值包含" https:",冒号也会在解析时产生问题。 请提出一些其他解析json的方法。

检查"来源"作为json的一个关键,例如上面定义的问题。 提前完成。

使用的语言是java

代码是

package jsonParser;

import java.io.FileReader;
import java.util.Map;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;

import net.minidev.json.*;
import net.minidev.json.annotate.JsonIgnore;
import net.minidev.json.parser.*;



public class parse {
       public static void main(String[] args) throws FileNotFoundException, Exception, ParseException 
       {
           JSONParser parser = new JSONParser(JSONParser.ACCEPT_NON_QUOTE|JSONParser.ACCEPT_SIMPLE_QUOTE);
            Object ob= parser.parse(new FileReader("c:\\dailymotion2.3.json"));
            JSONObject jsonObject=(JSONObject)ob;
            JSONArray status=(JSONArray)jsonObject.get("statuses");
           for (Object o : status)
           {
             JSONObject person = (JSONObject) o;

             String text = (String) person.get("text");
             System.out.print(text);

            System.out.print("   ^^^   ");

             JSONObject entities= (JSONObject) person.get("entities");
             //System.out.println(city);
             JSONArray url=(JSONArray) entities.get("urls");

             for(Object c : url)
             {
                 JSONObject ob1=(JSONObject)c;
                 System.out.print((String)ob1.get("url")+" ");
             }

             System.out.print("   ^^^   ");


             JSONArray hash=(JSONArray)entities.get("hashtags");
             for(Object c : hash)
             {
                 JSONObject ob2=(JSONObject)c;
                 System.out.print((String)ob2.get("text")+" ");
             }

             System.out.println("");

           /*String job = (String) person.get("job");
             System.out.println(job);

             JSONArray cars = (JSONArray) person.get("cars");

             for (Object c : cars)
             {
               System.out.println(c+"");
             }*/

           }

       }
}

json的样本

    {
  "statuses": [
    {
      "metadata": {
        "result_type": "recent",
        "iso_language_code": "en"
      },
      "created_at": "Sat Mar 08 10:22:02 +0000 2014",
      "id": 442243838338277400,
      "id_str": "442243838338277376",
      "text": "Esperanto video: Interview with a Buddhist monk and tour of monastery- http-//t.co/VrcZr5ASC4",
      "source": "<a href="https-//mobile.twitter.com" rel="nofollow">Mobile Web (M2)</a>",
      "truncated": false,
      "in_reply_to_status_id": null,
      "in_reply_to_status_id_str": null,
      "in_reply_to_user_id": null,
      "in_reply_to_user_id_str": null,
      "in_reply_to_screen_name": null,
      "user": {
        "id": 158791583,
        "id_str": "158791583",
        "name": "Kiara Noriega",
        "screen_name": "Doloresn73",
        "location": "Philadelphia",
        "description": "Exportador .......Interés en: twitteando",
        "url": null,
        "entities": {
          "description": {
            "urls": []
          }
        },
        "protected": false,
        "followers_count": 71,
        "friends_count": 626,
        "listed_count": 0,
        "created_at": "Wed Jun 23 17:19:51 +0000 2010",
        "favourites_count": 0,
        "utc_offset": null,
        "time_zone": null,
        "geo_enabled": false,
        "verified": false,
        "statuses_count": 294,
        "lang": "en",
        "contributors_enabled": false,
        "is_translator": false,
        "is_translation_enabled": false,
        "profile_background_color": "EBEBEB",
        "profile_background_image_url": "http-//pbs.twimg.com/profile_background_images/378800000169397295/EfYmokw8.jpeg",
        "profile_background_image_url_https": "https-//pbs.twimg.com/profile_background_images/378800000169397295/EfYmokw8.jpeg",
        "profile_background_tile": false,
        "profile_image_url": "http-//pbs.twimg.com/profile_images/1017448055/993_normal.jpg",
        "profile_image_url_https": "https-//pbs.twimg.com/profile_images/1017448055/993_normal.jpg",
        "profile_banner_url": "https-//pbs.twimg.com/profile_banners/158791583/1389487831",
        "profile_link_color": "990000",
        "profile_sidebar_border_color": "DFDFDF",
        "profile_sidebar_fill_color": "F3F3F3",
        "profile_text_color": "333333",
        "profile_use_background_image": true,
        "default_profile": false,
        "default_profile_image": false,
        "following": false,
        "follow_request_sent": false,
        "notifications": false
      },
      "geo": null,
      "coordinates": null,
      "place": null,
      "contributors": null,
      "retweet_count": 0,
      "favorite_count": 0,
      "entities": {
        "hashtags": [],
        "symbols": [],
        "urls": [
          {
            "url": "http-//t.co/VrcZr5ASC4",
            "expanded_url": "http-//dai.ly/xzew0p/143850",
            "display_url": "dai.ly/xzew0p/143850",
            "indices": [
              71,
              93
            ]
          }
        ],
        "user_mentions": []
      },
      "favorited": false,
      "retweeted": false,
      "possibly_sensitive": false,
      "lang": "en"
    }
   ]
}

1 个答案:

答案 0 :(得分:0)

这不是有效的JSON,因为您可以在http://jsonlint.com/

进行验证
"source": "<a href="https-//mobile.twitter.com" rel="nofollow">Mobile Web (M2)</a>"

值字符串中有嵌套引号。