为Silverlight 提供将Json解析为Geometry的方法

本文介绍了如何在Silverlight中实现一个自定义方法,用于解析JSON字符串为Geometry对象,适用于SOE开发场景。通过实例演示了如何解析点、线、面等几何对象,并设置空间参考信息。
在做SOE开发的时候,我们往往返回的是一个集合对象的Json字符串,可是Silverlight中并没有为我们提供解析该字符串的方法,为此我自己写了一个,因为后台代码正在测试,所以将前端的Json格式解析为Silverlight中的Geometry对象如下,如有疑问,请跟我联系。
 /// <summary>
        /// 将返回的json解析为Geometry,不考虑坐标包含M和Z,如果考虑,请改动即可。将ArcObjects的Geometry转为json的代码我正在测试。
        /// 作者:刘宇
        /// 时间2012年
        /// </summary>
        /// <param name="jsonResponse"></param>
        /// <returns></returns>

        private ESRI.ArcGIS.Client.Geometry.Geometry ParsefromJson(string jsonResponse)
        {

            JsonObject jsonObject = JsonObject.Parse(jsonResponse.ToString()) as JsonObject;
            SpatialReference pSpatial = new SpatialReference();
            ESRI.ArcGIS.Client.Geometry.Geometry pGeo = null;

            if (jsonObject.ContainsKey("geometries"))
            {

               
                JsonObject jsonObjectGeo = jsonObject["geometries"] as JsonObject;
                //空间参考信息
                if (jsonObjectGeo.ContainsKey("spatialReference"))
                {
                    pSpatial = this.myMap.SpatialReference;

                   
                 //JsonObject pSpatialJson =jsonObjectGeo["spatialReference"] as JsonObject;

                 //   根据需要添加                 
                }
                //点线面对象,不考虑hasz和hasM
                if (jsonObjectGeo.ContainsKey("points"))
                {
                    JsonValue JsonPoints = jsonObjectGeo["points"];

                    if (JsonPoints is JsonArray)
                    {
                        if (JsonPoints.Count == 1)
                        {
                            MapPoint pPoint = new MapPoint();

                            //去掉中括号

                            string[] pStrPoints = JsonPoints[0].ToString().Substring(1, JsonPoints[0].ToString().Length - 2).Split(',');

                            pPoint.X = Convert.ToDouble(pStrPoints[0]);
                            pPoint.Y = Convert.ToDouble(pStrPoints[1]);

                            pGeo = pPoint;
                          

                        }
              
                    }
                }
                else if (jsonObjectGeo.ContainsKey("paths"))
                {
                    JsonValue JsonPoints = jsonObjectGeo["paths"];

                    ESRI.ArcGIS.Client.Geometry.Polyline pPolyline = new ESRI.ArcGIS.Client.Geometry.Polyline();


                    ObservableCollection<ESRI.ArcGIS.Client.Geometry.PointCollection> pPointCollection = new ObservableCollection<ESRI.ArcGIS.Client.Geometry.PointCollection>();
                    // pPolyline.Paths

                    if (JsonPoints is JsonArray)
                    {
                        for (int i = 0; i < JsonPoints.Count; i++)
                        {
                            if (JsonPoints[i] is JsonArray)
                            {
                                ESRI.ArcGIS.Client.Geometry.PointCollection pPointCollections = new ESRI.ArcGIS.Client.Geometry.PointCollection();

                                JsonArray pInnerPoints = JsonPoints[i] as JsonArray;
                                for (int j = 0; j < pInnerPoints.Count; j++)
                                {
                                    string pStr = pInnerPoints[j].ToString();

                                    string[] pStrPoints = pInnerPoints[j].ToString().Substring(1, pInnerPoints[j].ToString().Length - 2).Split(',');
                                    MapPoint pPoint = new MapPoint();
                                    pPoint.X = Convert.ToDouble(pStrPoints[0]);
                                    pPoint.Y = Convert.ToDouble(pStrPoints[1]);

                                    pPointCollections.Add(pPoint);
                                }

                                pPointCollection.Add(pPointCollections);

                            }
                        }

                        pPolyline.Paths = pPointCollection;

                        pGeo = pPolyline;
                    }
                }
                else if (jsonObjectGeo.ContainsKey("rings"))
                {
                     JsonValue JsonPoints = jsonObjectGeo["rings"];

                ESRI.ArcGIS.Client.Geometry.Polygon pPolygon = new ESRI.ArcGIS.Client.Geometry.Polygon();



                ObservableCollection<ESRI.ArcGIS.Client.Geometry.PointCollection> pPointCollection = new ObservableCollection<ESRI.ArcGIS.Client.Geometry.PointCollection>();
   

                if (JsonPoints is JsonArray)
                {
                    for (int i = 0; i < JsonPoints.Count; i++)
                    {
                        if (JsonPoints[i] is JsonArray)
                        {
                            ESRI.ArcGIS.Client.Geometry.PointCollection pPointCollections = new ESRI.ArcGIS.Client.Geometry.PointCollection();

                            JsonArray pInnerPoints = JsonPoints[i] as JsonArray;
                            for (int j = 0; j < pInnerPoints.Count; j++)
                            {
                                string pStr = pInnerPoints[j].ToString();

                                string[] pStrPoints = pInnerPoints[j].ToString().Substring(1, pInnerPoints[j].ToString().Length - 2).Split(',');
                                MapPoint pPoint = new MapPoint();
                                pPoint.X = Convert.ToDouble(pStrPoints[0]);
                                pPoint.Y = Convert.ToDouble(pStrPoints[1]);

                                pPointCollections.Add(pPoint);
                            }

                            pPointCollection.Add(pPointCollections);

                        }
                    }

                    pPolygon.Rings= pPointCollection;

                    pGeo = pPolygon;

                }
                }
            }



           pGeo.SpatialReference = pSpatial;

            return pGeo;
        }


 

解析JSON中的`geometry_msgs/PoseWithCovariance`数据类型,需要了解该消息的结构。`geometry_msgs/PoseWithCovariance`包含一个`geometry_msgs/Pose`和一个长度为36的双精度浮点数组`covariance`,而`geometry_msgs/Pose`又包含`geometry_msgs/Point`和`geometry_msgs/Quaternion`。 以下是使用C++和`nlohmann/json`库进行解析的示例代码: ```cpp #include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; // 定义Point结构体 struct Point { double x; double y; double z; }; // 定义Quaternion结构体 struct Quaternion { double x; double y; double z; double w; }; // 定义Pose结构体 struct Pose { Point position; Quaternion orientation; }; // 定义PoseWithCovariance结构体 struct PoseWithCovariance { Pose pose; double covariance[36]; }; // 从JSON解析Point void from_json(const json& j, Point& p) { j.at("x").get_to(p.x); j.at("y").get_to(p.y); j.at("z").get_to(p.z); } // 从JSON解析Quaternion void from_json(const json& j, Quaternion& q) { j.at("x").get_to(q.x); j.at("y").get_to(q.y); j.at("z").get_to(q.z); j.at("w").get_to(q.w); } // 从JSON解析Pose void from_json(const json& j, Pose& pose) { j.at("position").get_to(pose.position); j.at("orientation").get_to(pose.orientation); } // 从JSON解析PoseWithCovariance void from_json(const json& j, PoseWithCovariance& pwc) { j.at("pose").get_to(pwc.pose); auto cov = j.at("covariance").get<std::vector<double>>(); for (size_t i = 0; i < 36; ++i) { pwc.covariance[i] = cov[i]; } } int main() { std::string json_str = R"( { "pose": { "position": { "x": 1.0, "y": 2.0, "z": 3.0 }, "orientation": { "x": 0.0, "y": 0.0, "z": 0.0, "w": 1.0 } }, "covariance": [ 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] } )"; json j = json::parse(json_str); PoseWithCovariance pwc = j.get<PoseWithCovariance>(); std::cout << "Position: (" << pwc.pose.position.x << ", " << pwc.pose.position.y << ", " << pwc.pose.position.z << ")" << std::endl; std::cout << "Orientation: (" << pwc.pose.orientation.x << ", " << pwc.pose.orientation.y << ", " << pwc.pose.orientation.z << ", " << pwc.pose.orientation.w << ")" << std::endl; std::cout << "Covariance:" << std::endl; for (int i = 0; i < 36; ++i) { std::cout << pwc.covariance[i] << " "; } std::cout << std::endl; return 0; } ``` ### 代码说明 1. **结构体定义**:定义了`Point`、`Quaternion`、`Pose`和`PoseWithCovariance`结构体,用于存储解析后的数据。 2. **JSON解析函数**:为每个结构体定义了`from_json`函数,用于从JSON对象中提取数据并赋值给结构体成员。 3. **主函数**:创建一个JSON字符串,解析它并将其转换为`PoseWithCovariance`对象,最后输出解析结果。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值